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.
prototypeproperty. They are not the same. If you read the ECMAScript spec, prototype is usually represented like [[Prototype]] which is an implementation detail lies in the JS engine rather than a language feature. However, in some engines [[Prototype]] can be accessed with__proto__property.prototype. Two different things. Yes, this is confusing. But they are related by the fact that the function'sprototypeproperty becomes the actual prototype for new objects created using that function as a constructor.__proto__is non-standard - there are better patterns.