I want to start writing better code and I have come to understand that putting everything relevant to a particular piece of functionality inside an object is a good idea.
Edit: I've tried to take on board the concept in @SoftwareEngineer171's answer. Now I have this:
var app = {
audioCtx : new(window.AudioContext || window.webkitAudioContext)(),
init : function () {
oscillator = app.audioCtx.createOscillator();
oscillator.type = 'square';
oscillator.frequency.value = 3000; // value in hertz
oscillator.start();
}
} // app object close
app.init();
But it doesn't work. Can anyone explain why?
I want to use this code as part of an application, but placed inside an object literal:
var context = new AudioContext(),
gainNode = context.createGain(),
oscillator = context.createOscillator();
gainNode.connect(context.destination);
oscillator.frequency.value = 440;
oscillator.connect(gainNode);
oscillator.start();
However, when I have tried, I run into problems of the kind mentioned here.
I want the code to begin like this:
var app = {
context : new AudioContext(),
...
}
Any help much appreciated as usual.