javascript - Advantages of setting the "constructor" Property in the "prototype" -


in javascript prototype inheritance, goal of adding prototype.constructor property. let me explain example.

 var super = function() {     this.superproperty = 'super property' } var sub = function() {     this.subproperty = 'sub property' }  sub.prototype = new super(); sub.prototype.constructor = sub; // advantages of statement  var inst = new sub(); 

the following lines return true in case, when adding sub.prototype.constructor = sub or not.

 console.log(inst instanceof sub)   // true console.log(inst instanceof super) // true 

i guess, may useful when getting new instances when and/or how?

thanks in advance.

it's reset constructor property accurately reflect function used construct object.

sub.prototype = new super();  console.log(new sub().constructor == sub); // -> 'false'   sub.prototype.constructor = sub; console.log(new sub().constructor == sub); // -> 'true'  

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -