Vote count:
0
I'm trying to fully grasp the MDN documentation on Object.create but I'm left with a few questions that I haven;t able able to crack.
Here is the example code MDN provides.
function Shape() {
this.x = 0;
this.y = 0;
}
// superclass method
Shape.prototype.move = function(x, y) {
this.x += x;
this.y += y;
console.info("Shape moved.");
};
// Rectangle - subclass
function Rectangle() {
Shape.call(this); // call super constructor.
}
// subclass extends superclass
Rectangle.prototype = Object.create(Shape.prototype);
Rectangle.prototype.constructor = Rectangle;
var rect = new Rectangle();
One line I don't understand is Shape.call(this). I've looked up Object.call(this) but I couldn't find any documentation on it. What is this line of code doing?
Also, this line: Rectangle.prototype.constructor = Rectangle; What's the benefit of setting the constructor to Rectangle?
Finally, why would somebody implement use Object.create() over doing something like this:
Rectangle.prototype = new Shape()
Thanks!
asked 1 min ago
Aucun commentaire:
Enregistrer un commentaire