I tried to use the following code to add a start
method to an object:
var Bounce = Bounce || {
Info : {},
Game : {}
};
Bounce.Game.prototype.start = function() {
Bounce.log("Starting " + new Bounce.Info());
}
But this results in the following error (on the Bounce.Game.prototype.start
line):
Uncaught TypeError: Cannot set property 'start' of undefined
Looking at the object in Chrome's console, I can see that it doesn't contain the prototype
object (but has toString
, valueOf
and constructor
etc.).
This is easily fixed by adding the following line before the prototype access:
Bounce.Game = function() {};
I don't know why this is necessary when the object has already been initialized?
W3Schools tells me "Every JavaScript object has a prototype", but that doesn't appear to be the case.