Results 1 to 2 of 2

Thread: Why constructor is not available from instances?

  1. #1
    Sencha Premium User
    Join Date
    Nov 2010
    Vote Rating

    Default 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.

    function Test(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.
    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
    Vote Rating


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

    Try the following:

    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.

Posting Permissions

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