3

次のコードで、instanceof が Shape と Rectangle の両方に対して false を返すのはなぜですか? また、rec の独自のプロパティにスーパークラスの x と y の両方が含まれるのはなぜですか?

    function Shape(x, y) {
        this.x=x;
        this.y=y;
    }
    Shape.prototype.move = function (x, y) {
        this.x += x;
        this.y += y;
        console.log("x = " + this.x + " y = " + this.y);
    };
    function Rectangle(x, y, w, h) {
        Shape.call(this, x, y);
        this.w = w;
        this.h = h;
    }
    Rectangle.prototype = Object.create(Shape.prototype);
    Rectangle.prototype.area = function() {
        return this.w * this.h;
    };
    var rec = new Rectangle(0,0,10,10);
    console.log("instanceof = " + rec instanceof Shape);
    console.log("instanceof = " + rec instanceof Rectangle);
    rec.move(2,3);
    console.log("area = " + rec.area());
    console.log(Object.getOwnPropertyNames(rec));
4

1 に答える 1