javascript - What makes the object.prototype to the constructor function if he has no name? -


this question has answer here:

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

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -