Results 1 to 3 of 3

Thread: Ext.extend(Object, {...}) not working as expected

  1. #1
    Sencha User sksoft's Avatar
    Join Date
    Jan 2008
    Location
    Moscow, Russia
    Posts
    34

    Default Ext.extend(Object, {...}) not working as expected

    How do you think, what is the value of result in this code:
    Code:
    C = Ext.extend(Object, {
        p: 1
    });
    
    c = new C({
        p: 2
    });
    
    var result = c.p;
    I expected result == 2, but indeed it's 1 .
    Why and how to fix it?

  2. #2
    Sencha User hendricd's Avatar
    Join Date
    Aug 2007
    Location
    Long Island, NY USA
    Posts
    5,966

    Default

    Quote Originally Posted by sksoft View Post
    How do you think, what is the value of result in this code:
    Code:
    C = Ext.extend(Object, {
        p: 1
    });
    
    c = new C({
        p: 2
    });
    
    var result = c.p;
    I expected result == 2, but indeed it's 1 .
    Why and how to fix it?
    new class C's constructor has no idea what to do with its argument { p:2}.

    That up to you:

    Code:
    C = Ext.extend(Object, {
      constructor : function(options){
    
         Ext.apply(this, options || {} );
    
      },
      p: 1
    });
    
    //Extend again:
    
    D = Ext.extend(C, {
      constructor : function(options){
    
         D.superclass.constructor.call(this, options);  //call the base constructor
         this.R = this.Q + this.p;
    
      },
      Q: 2
    });
    
    console.log( new D({p:3}).R );  //should now equal 5
    "be dom-ready..."
    Doug Hendricks

    Maintaining ux: ManagedIFrame, MIF2 (FAQ, Wiki), ux.Media/Flash, AudioEvents, ux.Chart[Fusion,OFC,amChart], ext-basex.js/$JIT, Documentation Site.


    Got Sencha licensing questions? Find out more here.


  3. #3
    Sencha User sksoft's Avatar
    Join Date
    Jan 2008
    Location
    Moscow, Russia
    Posts
    34

    Default

    Suggestion to modify Ext.extend to supply default constructor in such cases:
    Code:
    sb = overrides.constructor != oc ? overrides.constructor : function() {
        // provide default constructor for cases like: new Class({prop: value})
        if (sp === Object && arguments.length == 1 && typeof arguments[0] == 'object')
            Ext.apply(this, arguments[0]);
        else
            sp.apply(this, arguments);
    };

Posting Permissions

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