- Ext JS 3.0 Cookbook
- Jorge Ramon
- 257字
- 2021-04-01 13:43:45
Preventing naming conflicts and scoping non-global variables
Naming conflicts and scoping problems increase as applications gain size, and you start to work with multiple code files and modules. In Ext JS, you can resolve these issues by creating namespaces where you can logically organize your code.
How to do it...
The following steps will show how to create a namespace and "hide" local variables in it. These variables will not collide with similarly-named variables that are stored in other namespaces or have global scope:
- Define a namespace for the variables that are not global:
Ext.namespace('ExtJSCookbook.Samples');
- Create a local variable and a global variable with the same name:
Ext JSCookbook.Samples.var1 = 'var1 (local)'; // ExtJSCookbook.Samples.var1 is limited to the ExtJSCookbook.Samples namespace var var1 = 'var1 (global)'; // var1 is a global variable
- Prevent name collisions by putting any custom types inside the namespace you created:
// A custom type inside the Ext JSCookbook.Samples namespace ExtJSCookbook.Samples.Person = function() { return { firstName: '', lastName: '', show: function() { alert(this.firstName + ' ' + this.lastName); } } } var person1 = new Ext JSCookbook.Samples.Person(); person1.firstName = 'Jorge'; person1.lastName = 'Ramon'; person1.show();
Ext.namespace(namespace1, namespace2, namespace3,...)
and its shorthand Ext.ns(...)
allow you to create an arbitrary number of namespaces that you can use to scope variables and classes that are not global. For example, have a look at the following piece of code:
Ext.namespace('MyApplication', 'MyApplication.UI', 'MyApplication.Data', 'MyApplication.Services');
This namespace's definition above is equivalent to the following statements:
MyApplication = {}; MyApplication.UI = {}; MyApplication.Data = {}; MyApplication.Services = {};
- 基于元胞自動(dòng)機(jī)的城市路網(wǎng)交通流建模與仿真
- SolidWorks 2008機(jī)械設(shè)計(jì)一冊(cè)通
- Autodesk Ecotect Analysis 2011綠色建筑分析應(yīng)用
- Photoshop CC從入門到精通(全彩超值版)
- Web 2.0 Solutions with Oracle WebCenter 11g
- Excel公式、函數(shù)與圖表案例實(shí)戰(zhàn)從入門到精通(視頻自學(xué)版)
- OpenCart 1.4 Template Design Cookbook
- Photoshop CS6中文版基礎(chǔ)與實(shí)例教程(第6版)
- 中文版3ds Max/VRay效果圖制作完全自學(xué)教程(實(shí)例培訓(xùn)教材版)
- ASP.NET Core 3從入門到實(shí)戰(zhàn)
- JBoss RichFaces 3.3
- CorelDRAW X6平面設(shè)計(jì)與制作案例教程
- 企業(yè)微信公眾平臺(tái)開發(fā)實(shí)戰(zhàn):再小的個(gè)體也有自己的品牌
- Inkscape Starter (Microcontent)
- MODx Web Development