I'm reading a book on OOP javascript and got stuck on one of the examples.
In the first version of the sample code, a new instance of the Shape
constructor function is created and the toString
method is called. The toString
method returns "Triangle" which is the name
property of the Triangle
prototype, even though "Shape" is supposed to be the name
property of the Shape
prototype.
In the second version of the sample code, the toString
method returns "Shape" as intended.
I'm wondering how the changes in the second version of the code made any difference because it seems like in both versions of the sample code, the Triangle
prototype holds a reference to the Shape
prototype so the name
property of the Shape
prototype would still be updated to "Triangle" because of Triangle.prototype.name = "Triangle";
first version: jsfiddle
second version: jsfiddle