www.webdeveloper.com
Results 1 to 4 of 4

Thread: not clear on concept

  1. #1
    Join Date
    Feb 2010
    Location
    London
    Posts
    17

    not clear on concept

    Hi - In am learning Javascript and trying to understand why,

    the following line :
    Code:
    arguments.callee.superclass
    cannot be replaced by :
    Code:
    this.constructor.superclass
    In the following code sample :

    ( the explanation given is that if someone creates a subclass of PositionedRectangle, then this.constructor will refer to the new subclass constructor, not to PositionedRectangle - but why? 'this' here to my knowledge represents the object 'PositionRectangle' and if not I can't understand why not. )

    Code:
    // Here is a simple Rectangle class.
    // It has a width and height and can compute its own area
    function Rectangle(w, h) {
        this.width = w;
        this.height = h;
    }
    
    
    Rectangle.prototype.area = function( ) { return this.width * this.height; }
    
    
    // Here is how we might subclass it
    function PositionedRectangle(x, y, w, h) {
        // First, invoke the superclass constructor on the new object
        // so that it can initialize the width and height.
        // We use the call method so that we invoke the constructor as a
        // method of the object to be initialized.
        // This is called constructor chaining.
        PositionRectangle.superclass = Rectangle;
        arguments.callee.superclass.call(this, w, h);
    
        // Now store the position of the upper-left corner of the rectangle
        this.x = x;
        this.y = y;
    }
    
    
    // Create a prototype for the subclass that inherits from the prototype
    // of the superclass.
    
    function heir(p) {
          function f(){}
          f.prototype = p;
          return new f();
    }
    PositionRectangle.prototype = heir(Rectangle.prototype);
    
    PositionRectangle.prototype.constructor = PositionRectangle;

  2. #2
    Join Date
    Feb 2010
    Location
    London
    Posts
    17
    I managed to figure it out.

    the explanation which was offered and when looked at from the understanding that the logic in question will go in the subclass of PositionedRectangle and not in PositionedRectangle itself is the explicit key to understanding this.

  3. #3
    Join Date
    Feb 2010
    Location
    London
    Posts
    17
    I guess I am confused again, not sure what the correct explanation is.
    I would be grateful if someone could help me out because at the moment I am the only one on this thread trying to figure out how to answer my own questions.......

  4. #4
    Join Date
    Feb 2010
    Location
    London
    Posts
    17
    ...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles