[removed] find the prototype object to which a property belongs

前端 未结 2 1483
清酒与你
清酒与你 2021-01-06 06:21

I have an instance from Square which inherits from Rectangle

instance instanceof Rectangle --> true
instance instanceof Square    --> true
instance.are         


        
2条回答
  •  渐次进展
    2021-01-06 07:05

    Replying to you comment: What you essentially seem to want is to call a function of the base class inside the overriding function of an inherited class.

    I wouldn't bother with the prototype chain in your case, you can just build base into your inheritance model:

    function Rectangle() {}
    Rectangle.prototype.area = function () {
        console.log("rectangle");
    };
    
    //setting up inheritance
    function Square() {}
    Square.prototype = Object.create(Rectangle.prototype);
    Square.prototype.base = Rectangle.prototype;
    
    Square.prototype.area = function () {
        this.base.area();
        console.log("square");
    };
    
    var square = new Square();
    square.area();
    

    FIDDLE

提交回复
热议问题