javascript 如何选择包含类的Object的属性

6gpjuf90  于 2022-12-10  发布在  Java
关注(0)|答案(1)|浏览(133)

是否可以从包含如下类的对象中选择属性?

//Object
const voices = {
  fmsynth: Tone.FMSynth,
  amsynth: Tone.AMSynth,
  synth: Tone.Synth
}

//my function to select the above synths
switch_synth(synth_id) {
   const synth = new Tone.PolySynth(voices[synth_id],  6).toDestination();
   console.log(voices[synth_id]);
}
vc9ivgsu

vc9ivgsu1#

是的,只要你用适当的键调用它

在代码中,调用voices["fmsynth"]甚至voices.fmsynth,以获取Tone.FMSynth
因此,只要使用适当的synth_id调用switch_synth,代码似乎做了正确的事情,例如:

switch_synth("fmsynth")

是否出现错误?如果出现,是什么错误?

相关问题