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:
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:
instead
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:
Which is not possible with:
参考:
[url]http://www.sencha.com/forum/showthread.php?127671-Ext.create-vs-the-keyword-new[/url]
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});
参考:
[url]http://www.sencha.com/forum/showthread.php?127671-Ext.create-vs-the-keyword-new[/url]
本文探讨了Ext.create与new关键字的区别,强调了从ExtJS 4开始使用Ext.create的优势,包括依赖项解析更容易、自动加载目标类、方便实例化ExtJS库中的组件以及调试更加简单。
1008

被折叠的 条评论
为什么被折叠?



