0

i am new to JS and quite confused in this topic. now i am reading a book and it says :-

where all prototypes point to the same object and the parents get children's properties, is to use an intermediary to break the chain. The intermediary is in the form of a temporary constructor function.

Creating an empty function F() and setting its prototype to the prototype of the parent constructor, allows you to call new F() and create objects that have no properties of their own, but inherit everything from the parent's prototype.

Here is the code.

  function Shape() {

}
// augment prototype
Shape.prototype.name = 'Shape';
Shape.prototype.toString = function () {
    return this.name;
};




function TwoDShape() {

};
// take care of inheritance
// Creating a Temporary constructor Function
var F = function () {

};


// Then Setting its prototype to Parent's Prototype.
F.prototype = Shape.prototype;


// Allows to call a function
TwoDShape.prototype = new F();

//Settubg cobstryctir
TwoDShape.prototype.constructor = TwoDShape;


// augment prototype
TwoDShape.prototype.name = '2D shape';




function Triangle(side, height) {
    this.side = side;
    this.height = height;
}




var F = function() {

};
F.prototype = TwoDShape.prototype;
Triangle.prototype = new F();
Triangle.prototype.constructor = Triangle;
// augment prototype
Triangle.prototype.name = 'Triangle';
Triangle.prototype.getArea = function () {
    return this.side * this.height / 2;
};


var my = new Triangle(5, 10);
console.log(my.getArea());

Now i am confused that line

allows you to call new F() and create objects that have no properties of their own, but inherit everything from the parent's prototype.

Triangle.prototype = new F();

means when we create a triangle obj it's prototype points to new Object right? and then

F.prototype = Shape.prototype;

Which means f prototype points to Shape prototype.. so indirectly the new object of triangle is using shape prototype. But what is the use?? Why are doing this? Please bear with me as i am a learner. thanks.

Cloudboy22
  • 1,496
  • 5
  • 21
  • 39
  • As you self wrote, this would be to keep the `prototype` of `Shape`, but keep no properties of `Shape` when creating your `Triangle`. – Arg0n Nov 25 '15 at 10:49
  • Argh. Throw that book away, it's massively outddated. What the author tries to introduce here, mixed with his explanation of inheritance in the prototype model, is a workaround for environments that lack [`Object.create`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create). – Bergi Nov 25 '15 at 10:49
  • @Bergi seriously i mean this thing should not be reading this?? – Cloudboy22 Nov 25 '15 at 10:51
  • @Arg0n please explain in detail can you? – Cloudboy22 Nov 25 '15 at 10:52
  • @MarcAndreJiacarrini: Well I don't know what book you are reading, and it might be quite good actually (though then you wouldn't need to ask this question), but you should know that at least this particular technique is deprecated. Be wary. – Bergi Nov 25 '15 at 10:54
  • @Bergi ok i am reading http://www.amazon.com/Object-Oriented-JavaScript-2nd-Stoyan-Stefanov/dp/1849693129/ref=sr_1_2?s=books&ie=UTF8&qid=1448448625&sr=1-2&keywords=object+oriented+programming+in+javascript – Cloudboy22 Nov 25 '15 at 10:54
  • @Bergi so what is the new way? – Cloudboy22 Nov 25 '15 at 10:55
  • Just `TwoDShape.prototype = Object.create(Shape.prototype);`. No `F` at all. I guess [this answer](http://stackoverflow.com/a/17393153/1048572) would be a good read for you. – Bergi Nov 25 '15 at 10:57

0 Answers0