My following code is this:
var triangle = {a:1, b:2, c:3};
function constructorFunction() {
this.color = "red";
}
constructorFunction.prototype = triangle;
I know that the prototype keyword extends classes in this syntax: Object.prototype.method = function() {} But what for is this example? after constructorFunction.prototype there is no property or method name, what happens here?
That's not true. The
prototypeof the constructor is set usingtriangleobject. Theprototypehas 3 properties.prototypeis an object. Consider these examples:In the above snippet the
objvariable's value is reset using an object literal.In the above example the original object is extended by using dot notation.
That's because you haven't created an instance. The
ais property of the constructor'sprototypeobject.If you create an instance object then
ais a property of that instance.