LoginSignup
0
2

More than 1 year has passed since last update.

JavaScript/TypeScriptで変数で指定したオブジェクトのメソッドを発火させる

Posted at

JavaScript(TypeScript)でのオブジェクトのプロパティへのアクセスは以下のようにすることができます。

const o = {key: 'value'};
console.log(o.key)
console.log(o['key']); // これもまた動作する

const k = 'key';
console.log(o[k]); // これもまたOK

なので…

const o = {f: () => console.log('function')};
o.f();
o['f'](); // これもまた動作する

const k = 'f';
o[k](); // これもまたOK

こういうことができるようです。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2