0

Here is my little program. When I check the value of rec in the debug mode, the object is Base { x=0, y=0, w=10, more...}. Should it be Rectangle? Also the constructor.prototype is Base. Why not Shape?

    function Base() {
    }

    function Shape(x, y) {
        this.x = x;
        this.y = y;
    }

    Shape.prototype = new Base();
    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 = new Shape();
    Rectangle.prototype.area = function() {
        return this.w * this.h;
    };
    var rec = new Rectangle(0, 0, 10, 10);
    console.log(instanceOf(rec, Rectangle));

    function instanceOf(object, constructor) { 
        while (object != null) {
            if (object == constructor.prototype)
                return true;
            if ( typeof object == 'xml') {
                return constructor.prototype == XML.prototype;
            }
            object = object.__proto__;
        }
        return false;
    }
J Any
  • 4,847
  • 3
  • 13
  • 8

1 Answers1

0

Have a look at Why [not] to use the new keyword here?. You might not use it and create a new instance of it, but rather just inherit from Base.prototype.

Also the constructor.prototype is Base. Why not Shape?

I'm not sure which constructor you are referring to here:

  • The constructor property of all your objects is Base, as all of them inherit this prototype from the Base.prototype object. You did not overwrite it after setting up the inheritance chains. It is not really necessary, but good style: Shape.prototype.constructor = Shape and Rectangle.prototype.constructor = Rectangle - where those prototype objects are the overwritten ones which inherit from Base.

  • The constructor parameter of your instanceOf function. You pass in Rectangle there, so constructor.prototype is the prototype object of Rectangle, which inherits from Base but is different.

When I check the value of rec in the debug mode, the object is Base { x=0, y=0, w=10, more...}

Usually not. Is Base something special, e.g. a host object? Your rec object is an instance of Base, so it might be displayed differently because of that.

rec is just an object which inherits from Rectangle.prototype which inherits from Shape.prototype which inherits from Base.prototype which inherits from​… Assuming Base is the function you defined, from Object.prototype which inherits from null.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Yes, it works after adding constructors: Shape.prototype.constructor = Shape and Rectangle.prototype.constructor = Rectangle. I have one more question: why rec is a function, not an object? In the debug console, it is Function { x= 0 , y= 0 , w= 10 , more...} – J Any Nov 11 '12 at 17:44
  • One more question: after the change, rec.__proto__.property becomes an endless Rectangle chain. __proto__ Rectangle(x, y, w, h) prototype Rectangle(x, y, w, h) prototype Rectangle(x, y, w, h) prototype Rectangle(x, y, w, h) prototype Rectangle(x, y, w, h) prototype Rectangle(x, y, w, h) – J Any Nov 11 '12 at 17:59
  • I missed new in my constructor assignment. It works perfect now. Thanks! – J Any Nov 11 '12 at 18:16