1. #1
    Ext JS Premium Member
    Join Date
    Sep 2009
    Posts
    59
    Vote Rating
    3
    wsi is on a distinguished road

      0  

    Default Ext.create vs the keyword new

    Ext.create vs the keyword new


    We are currently revamping our coding standard to conform with the changes of EXTJS 4.0 and have come to point of confusion. I hope someone can clarify which is the better approach.

    Extjs 4.0 introduced Ext.Create which instantiates classes but in various parts of the API documents it makes references to using new. Take for example;

    Code:
    Ext.define('Person', {
         name: 'Unknown',
    
         constructor: function(name) {
             if (name) {
                 this.name = name;
             }
    
             return this;
         },
    
         eat: function(foodType) {
             alert("I'm eating: " + foodType);
    
             return this;
         }
    });
    
    var aaron = new Person("Aaron");
    aaron.eat("Sandwich"); // alert("I'm eating: Sandwich");
    I would have expected the last few lines to be
    Code:
    var aaron = Ext.create('Person', {
        name: "Aaron"
    });

    Why does the example not make use of Ext.create?

    Overall my question is, should Ext.create be used to instantiate a class or should the keyword new be used?

  2. #2
    Sencha User ykey's Avatar
    Join Date
    Mar 2010
    Location
    USA
    Posts
    245
    Vote Rating
    27
    ykey has a spectacular aura about ykey has a spectacular aura about

      0  

    Default


    Ext.create is an alias for Ext.ClassManager#instantiate. The docs say that if Ext.Loader is enabled and the class has not been defined yet, it will attempt to load the class for you via synchronous loading. It is my understanding that the dynamic loading will not happen if you just use the new keyword.

  3. #3
    Ext JS Premium Member westy's Avatar
    Join Date
    Feb 2009
    Location
    Bath, UK
    Posts
    796
    Vote Rating
    38
    westy is a jewel in the rough westy is a jewel in the rough westy is a jewel in the rough

      0  

    Default


    Lots of examples don't even use Ext.define either, and none of them to my knowledge use the new application architecture, so not sure looking in the examples is wise for 'best practice'

    I've been trying to use Ext.create myself (although just realised some of the code I posted a minute ago doesn't) for the simple reason that if the dynamic loading works well then it should be worth using, and wouldn't want to have to rework all the code that uses the new keyword to instantiate classes.

    Can't believe it's that much of a performance hit either really.

    Just my tuppence.

  4. #4
    Ext JS Premium Member
    Join Date
    Nov 2007
    Location
    Cologne, Germany
    Posts
    10
    Vote Rating
    7
    stefan.lotties is on a distinguished road

      0  

    Default


    So, is there a final decision here, or at least an official advice? Like "Use Ext.create when you need dynamic loading and new when you don't need it"?

  5. #5
    Ext JS Premium Member stevil's Avatar
    Join Date
    Nov 2007
    Location
    Denver, CO
    Posts
    1,045
    Vote Rating
    9
    stevil will become famous soon enough

      0  

    Default


    Quote Originally Posted by westy View Post
    Can't believe it's that much of a performance hit either really.

    Just my tuppence.
    Yeah, I'm thinking that Sencha is really soft-selling the dynamic class loading as a developer convenience. My app is very metadata-driven for its UI, to the point that, when a function is requested, we make Ajax calls to get the panel definition for the content, another for its tools (because that call is shared by all functions), and then another for the data. Gives us really fine grained control over how the content renders based on user permissions and the like.

    I'm thinking of overhauling that whole structure to use the new Class loader - would simplify my code greatly!

    stevil

  6. #6
    Sencha - Sencha Touch Dev Team Jacky Nguyen's Avatar
    Join Date
    Jul 2009
    Location
    Palo Alto, California
    Posts
    469
    Vote Rating
    13
    Jacky Nguyen has a spectacular aura about Jacky Nguyen has a spectacular aura about

      1  

    Default


    Ext.create provides a higher level control for instantiation throughout. It's best practice from Ext JS 4 onwards to use it over the 'new' keyword, since it's tightly coupled with Ext.Loader which makes dependency resolution much easier. During development the target class will be automatically loaded synchronously the first time it's used, which frees you from having to remember "Ext.require-ing" it before-hand.

    Besides, just like Ext.require, Ext.create accepts either a class name or an alias, which makes it extremely convenient to instantiate almost everything in Ext JS 4 library by its xtype / type without having to figure out its full class name up front, for example:

    Code:
    Ext.create('widget.combobox'); // instead of Ext.create('Ext.form.field.ComboBox')
    Ext.create('proxy.jsonp'); // instead of Ext.create('Ext.data.proxy.JsonP')
    Debugging is much more easier. If you try to instantiate a nonexistent class with the 'new' keyword, it's painful to figure out quickly what's going on with "TypeError: undefined is not a function", for example, try:

    Code:
    new Ext.data.proxy.JsonD
    Instead:

    Code:
    Ext.create('Ext.data.proxy.JsonD')
    gives you:

    Code:
    [Ext.create] Cannot create an instance of unrecognized class name / alias: Ext.data.proxy.JsonD
    We've heavily optimized it internally for the best performance possible, and extreme benchmarks prove no considerable hit on performance over thousands of objects.

    Another robust feature with Ext.create that can't be simply achieve with the 'new' keyword is it enables instantiation with variable arguments. For example:

    Code:
    Ext.create.apply(null, ['My.ClassName', arg1, arg2, arg3, ...]);
    Which is not possible with:

    Code:
    new My.ClassName({list of arguments must be known here});
    I hope that's enough reasons for everyone to ditch the old "new"
    Sencha Touch Lead Architect

  7. #7
    Ext JS Premium Member yyjia's Avatar
    Join Date
    Oct 2008
    Location
    Greater Boston Area
    Posts
    89
    Vote Rating
    0
    yyjia is on a distinguished road

      0  

    Default


    I think they have reason to use new operator sometimes when the object they defined is just a Simple Plain JS Object. I will use new operator if I just define some simple JS objects. It is not economical to let Ext.create function run though whole preprocessor and post porocessor when the object defined is just a simple object.

    Yiyu Jia

  8. #8
    Sencha - Ext JS Dev Team dongryphon's Avatar
    Join Date
    Jul 2009
    Posts
    1,230
    Vote Rating
    114
    dongryphon is a name known to all dongryphon is a name known to all dongryphon is a name known to all dongryphon is a name known to all dongryphon is a name known to all dongryphon is a name known to all

      0  

    Default


    Also debugging into operator new is a bit simpler than stepping through Ext.create.

    Even so, if there is any chance the class has not be loaded, Ext.create can fix things "just in time" and save you the debugging session or failed demo. Not that I've ever had that happen...

  9. #9
    Ext JS Premium Member stevil's Avatar
    Join Date
    Nov 2007
    Location
    Denver, CO
    Posts
    1,045
    Vote Rating
    9
    stevil will become famous soon enough

      0  

    Default


    Quote Originally Posted by dongryphon View Post
    Also debugging into operator new is a bit simpler than stepping through Ext.create.
    Agreed. The last version of my application used Ajax calls to dynamically load panel config/instantiation code. I'm quickly migrating it to use define classes the new Loader way, and then just Create them. VERY slick, and I don't have to put debugger statements in to get at my dynamic code!

    stevil

  10. #10
    Sencha User steffenk's Avatar
    Join Date
    Jul 2007
    Location
    Haan, Germany
    Posts
    2,649
    Vote Rating
    6
    steffenk has a spectacular aura about steffenk has a spectacular aura about steffenk has a spectacular aura about

      0  

    Default


    What's about the Ext.widget() or Ext.createWidget(), are these shortcuts just to shorten code, or do they have any (dis)advantage? I don't see them in examples.
    vg Steffen
    --------------------------------------
    Release Manager of TYPO3 4.5

Similar Threads

  1. Highlight keyword in drop down list
    By SimoAmi in forum Ext 3.x: Help & Discussion
    Replies: 3
    Last Post: 9 Dec 2009, 11:20 AM
  2. keyword this not work
    By igi in forum Ext 3.x: Help & Discussion
    Replies: 1
    Last Post: 23 Jun 2009, 5:16 AM
  3. Search based on Keyword
    By DevHasten in forum Ext 2.x: Help & Discussion
    Replies: 0
    Last Post: 8 May 2008, 10:16 PM
  4. [Solved]Binding a fuction's this keyword?
    By vendiddy in forum Ext 1.x: Help & Discussion
    Replies: 2
    Last Post: 14 Dec 2007, 5:07 PM

Thread Participants: 13

film izle

hd film izle

film sitesi

takipci kazanma sitesi

takipci kazanma sitesi

güzel olan herşey

takipci alma sitesi

komik eğlenceli videolar