1

I have following JavaScript code

var Car = function(){}

var c1 = new Car();

//Will throw an error as there is no run function
//c1.run()

//add run function to Car Prototype
Car.prototype.run = function() { return "running 1."};

//will return "running 1."
c1.run()

//Modify the run function
Car.prototype.run = function() { return "running 2."}

//will return running 2
c1.run()

//will return true
c1 instanceof Car

//Assign multiple functions via object literal
Car.prototype = { 
    run : function() { return "running 3."}, 
    openDoors : function() { return "opening doors."}
}

//c1.run still returning running 2
c1.run()

//returns false, c1 no longer connected to Car
c1 instanceof Car

//a new instance of Car will correctly point to 
//functions from updated prototype
var c2 = new Car()

//will return running 3
c2.run()

//C1 do not have "openDoors" method
//c1.openDoors()

//openDoors for C2 works
c2.openDoors()

Considering above code my questions and observations are

  • What is possible explanation of this? Seems like chain brakes (between c1 and its prototype) as soon as Car.prototype was overridden (as its a reference).
  • C1 maintains its copy of prototype via c1.__proto
  • Is it a good practice of assigning object literals to prototype?
  • I refereed this link for some explanation, but could not grasp most of it. The article also warns about Mutating [[Prototype]]

To add, the constructor of c1 is very well pointing to new updated prototype

//create a new instance with c1 constructor
var c3 = new c1.constructor()

//will return running 3
c3.run()
Ajay Bhosale
  • 1,872
  • 24
  • 35
  • Rule of thumb: Never instantiate constructors before their prototype declaration is done. Never assign to `.prototype` after instances have been created. – Bergi Jul 03 '14 at 12:49
  • The `[[prototype]]` (which you should not alter using `.__proto__`) is [a different thing](https://stackoverflow.com/questions/9959727/proto-vs-prototype-in-javascript) from the `constructor.prototype` we are speaking of. MDN docs for that are well hidden at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain – Bergi Jul 03 '14 at 12:57
  • @Bergi - thanks, I found one more [question](http://stackoverflow.com/questions/650764/how-does-proto-differ-from-constructor-prototype), which explains good difference between proto and prototype. – Ajay Bhosale Jul 03 '14 at 13:01
  • Considering that all my questions are answers via two different questions, should I delete my question? – Ajay Bhosale Jul 03 '14 at 13:01
  • To summarize my understanding its " __ proto __ " which keeps reference to actual prototype, in case i modify it to "c1. __ proto __ = {run : function(){return "1"}};", after calling "c1.run" it will return 1. But this is not a suggested practice as mentioned in the link above. – Ajay Bhosale Jul 03 '14 at 13:03
  • There's a comment on the "`__proto__` VS. prototype in JavaScript" question pointing to that, yes :-) | No, you should not delete it. It will help others to find those questions | Yes. – Bergi Jul 03 '14 at 13:06

0 Answers0