javascripttonejs

How to select a property of an Object containing a class


Is it possible to select a property from an object containing a class like this below?

//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]);
}

Solution

  • Yes, as long as you call it with the appropriate key

    In your code, call voices["fmsynth"] or even voices.fmsynth, to get Tone.FMSynth.

    So your code seems to be doing the right thing, as long as you are calling switch_synth with an appropriate synth_id, e.g.

    switch_synth("fmsynth")
    

    Are you getting an error, and if so, what error?