Search code examples
javascriptoopprototypejs

Get instance of parent of a class in constructor of class


var Shape = Class.create(new Element("div", {
    "class": "shape"
}), {
    //constructor of Shape
    initialize: function () {

    }
})

Shape class inherit from an instance of Element, all what I want to get to know is, how can I refer to this instance in constructor of Shape if I can?


Solution

  • http://jsfiddle.net/r585d/

    Use this to refer to the Element instance inside Shape constructor:

    this.constructor.prototype

    Alternatively, you could do this (Since it doesn't make sense for a shape to inherit from element):

    var Shape = (function() {
        var elem = new Element("div", {
            "class": "shape"
        });
        return Class.create({
            initialize: function() {
                //refer to elem here    
            }
        });
    })();