ブラウザ間で動的関数名を呼び出す方法は? 私はそれを行ういくつかの方法を見てきましたが、以下のように「エレガント」なものはありません。問題は、Chrome では機能しますが、Firefox や Safari では機能しません。
私が電話したら
const component = "a";
this[component]() // console: Called a
this[`${component}`]() // console: Called a
Chrome では正常に動作し、関数は正しく呼び出されます。Firefoxでは、
TypeError: this[("" + component)] is not a function
これを達成したい場合はどうすればよいですか?
編集してコンテキストを追加します
フレームワークはReactです。
例:
export default class MyComp extends Component {
a() {
console.log("Called a");
}
b() {
console.log("Called b");
}
c() {
console.log("Called c");
}
renderAThing(component) {
return this[component]();
}
render() {
return this.renderAThing("a");
}
}
render() で thiscomponent を直接呼び出すと、機能します。
EDIT 2これはトランスパイルの問題であり、ブラウザの問題ではないようです。ご指摘のとおり、コードは Chrome と Firefox で有効です。Meteor と Babel と一緒に React を使用しています。ヒントをくれた@Jaromanda Xに感謝します。
参考までに、縮小された (=実稼働) Meteor コードも Chrome では動作しません。