1. #1
    Touch Premium Member
    Join Date
    Nov 2010
    Location
    Chicago
    Posts
    1,323
    Vote Rating
    114
    LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light

      0  

    Default Why constructor is not available from instances?

    Why constructor is not available from instances?


    I'm trying to understand why the constructor property is not available from component instances.

    Is this a security feature? Why do I have to call Ext.getClass?

    See the code. I think it's self-explanatory.

    Code:
    function Test(name) {
        this.name = name;
    }
    var obj = new Test('Joe');
    obj.constructor === Test // true (makes sense)
    
    var cmp = new Ext.Component();
    cmp.constructor === Ext.Component; // false (why is it false?)
    Ext.getClass(cmp) === Ext.Component // true (why do I have to do this?)
    I thought Ext.define would add a constructor property to the constructor's prototype to make the constructor available from instances.

    I know that self works, but why the property self is used in place of constructor?
    BTW the name self is not intuitive. It indicates an object, not a constructor.
    Code:
    var cmp = new Ext.Component();
    cmp.self === Ext.Component; // true (why self is used?)
    Last edited by LesJ; 3 Mar 2012 at 5:37 AM. Reason: Add self info

  2. #2
    Sencha Premium Member skirtle's Avatar
    Join Date
    Oct 2010
    Location
    UK
    Posts
    3,592
    Vote Rating
    323
    skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future

      0  

    Default


    I can't speak for why but I can maybe attempt to rationalise it.

    Try the following:

    Code:
    cmp.constructor === Ext.Component.prototype.constructor; // true
    console.log(Ext.Component); // depending on your browser, this will show the source code for this function
    If you think of Ext.Component as the class rather than as the constructor then it makes sense I think. It's a bit like the scenario in Java where calling new Blah() is performed on a class name but implicitly calls an internal constructor.

Thread Participants: 1