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
prototype
of the constructor is set usingtriangle
object. Theprototype
has 3 properties.prototype
is an object. Consider these examples:In the above snippet the
obj
variable'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
a
is property of the constructor'sprototype
object.If you create an instance object then
a
is a property of that instance.