0

Is there a way to get rid of the this keyword on the line:

this.getOscillatorConfig(oscNumber);

below?:

const oscPlayer = (audioContext, voiceConfig) => ({

    getOscillatorConfig(oscNumber)
    {
        return voiceConfig.oscillators[oscNumber];
    },

    getOscillator(oscNumber)
    {
        this.getOscillatorConfig(oscNumber);

        let vco = audioContext.createOscillator();
        vco.type = oscConfig.waveform;

        return vco;
    },

    start: (vco, time, noteLength, frequency) => {

        vco.frequency.value = frequency;

        vco.start(time);
        vco.stop(time + noteLength);
    }

});

const octave = () => ({
        applyPipeLength: (frequency, pipeLength) => {
        return frequency / (parseInt(pipeLength, 10) / 8);
    }
});

const Voice = (audioContext, voiceConfig)  => {

    return Object.assign(
        {},
        oscPlayer(audioContext, voiceConfig),
        octave()
    )
}

If I don't use it, I have getOscillatorConfig is undefined.

Or any other advice for how to structure this?

1
  • 1
    "Is there a way..." Why do you want to get rid of the this? Commented Sep 19, 2016 at 11:33

1 Answer 1

1

To be able to omit this, you have to create a function with name getOscillatorConfig that is available in the scope you want to call it:

const oscPlayer = (audioContext, voiceConfig) => {

    function getOscillatorConfig(oscNumber) {
        return voiceConfig.oscillators[oscNumber];
    }

    return {
        getOscillator(oscNumber) {
            getOscillatorConfig(oscNumber);

            let vco = audioContext.createOscillator();
            vco.type = oscConfig.waveform;

            return vco;
        },

        start(vco, time, noteLength, frequency) {
            // ...
        }
    };
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.