javascript - What makes the object.prototype to the constructor function if he has no name? -
this question has answer here:
- defining javascript prototype 5 answers
my following code this:
var triangle = {a:1, b:2, c:3}; function constructorfunction() { this.color = "red"; } constructorfunction.prototype = triangle; i know prototype keyword extends classes in syntax: object.prototype.method = function() {} example? after constructorfunction.prototype there no property or method name, happens here?
after constructorfunction.prototype there no propertie or method name
that's not true. prototype of constructor set using triangle object. prototype has 3 properties.
prototype object. consider these examples:
var obj = { 'baz': 'bar' }; obj = { 'foo': 'bash' } // obj => { 'foo': 'bash' } in above snippet obj variable's value reset using object literal.
var obj = { 'baz': 'bar' }; obj.foo = 'bash'; // obj => { 'baz': 'bar', 'foo': 'bash' } in above example original object extended using dot notation.
i try console.log(constructorfunction.a); returns undefined.
that's because haven't created instance. a property of constructor's prototype object.
console.log(constructorfunction.prototype.a); if create instance object a property of instance.
var ins = new constructorfunction(); console.log(ins.a);
Comments
Post a Comment