当前位置:首页 > 开发 > Web前端 > Ext > 正文

Ext.create和new

发表于: 2013-10-04   作者:darrenzhu   来源:转载   浏览次数:
摘要: 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 mak
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:

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:

new Ext.data.proxy.JsonD


instead

Ext.create('Ext.data.proxy.JsonD')


gives you:
[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:
Ext.create.apply(null, ['My.ClassName', arg1, arg2, arg3, ...]);

Which is not possible with:
new My.ClassName({list of arguments must be known here});


参考:
http://www.sencha.com/forum/showthread.php?127671-Ext.create-vs-the-keyword-new

Ext.create和new

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
The skin:three—leaves glass...... There is a legend:if you can find a four—leaves glass a
数组有四种定义的方式 使用构造函数: var a = new Array(); var b = new Array(8); var c = new Ar
数组有四种定义的方式 使用构造函数: var a = new Array(); var b = new Array(8); var c = new Ar
C++平台有四种方式动态索求和释放内存 new和delete编译器帮你做的事情 <img style="background-i
单个class的new的重载 重载的 new、delete(或者 new[]、delete[])操作符必须是类的静态成员函数(
编程中有一种很难发现的错误是迷途指针。迷途指针也叫悬浮指针、失控指针,是党对一个指针进行delet
在定义泛型类时,可以对客户端代码能够在实例化类时用于类型参数的类型种类施加限制。如果客户端代
从2009年初开始动笔到最终定稿,这本书的编写花费了我一年多的时间,大大超出了先前预期的计划。当
Eclipse Forms: New in 3.3 Summary Eclipse Forms is a layer on top of SWT that allows you to a
Create New Commands in Tcl eryar@163.com 摘要Abstract:Tcl/Tk脚本可以很容易实现用户自定义的命
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号