Ext.namespace

Resolution of Ext .namespace namespace

When I was working on a project to see Ext 3.x, I thought it would be good to use it, but I didn't understand it in depth. I recently encountered this doubt when researching Sencha. So I searched the Internet for a lot of relevant information, and summarized it here.

1.

Ext .namespace method, the function of this method is to convert the incoming parameters into objects. The purpose of using this method is to distinguish classes with the same name, which is similar to the function of package in java. Let's look at the source code first :

 1 namespace : function(){ 
 2 var a=arguments, o=null, i, j, d, rt; 
 3 for (i=0; i<a.length; ++i) { 
 4                d=a[i].split("."); 
 5                rt = d[0]; 
 6                eval(‘if (typeof+ rt +=="undefined"){‘ + rt += {};} o =+ rt + ‘;‘); 
 7 for (j=1; j<d.length; ++j) { 
 8                     o[d[j]]=o[d[j]] || {}; 
 9                     o=o[d[j]]; 
10                } 
11        } 
12 , 
13 …… 
14 …… 
15 Ext.ns =Ext .namespace; 
16 …… 
17  …… 
18 Ext.ns("Ext", "Ext.util", "Ext.grid", "Ext.dd", "Ext.tree", "Ext.data", "Ext.form", "Ext.menu", "Ext.state", "Ext.lib", "Ext.layout", "Ext.app", "Ext.ux"); 

First get the parameters of the namespace method through arguments, then divide them into arrays with dots, and assign the empty objects recursively in turn. The above is the built-in namespace of the system.

As can be seen from the code, if the string parameter we pass in is separated by ".", then multiple objects will be created, such as:

Ext .namespace('system.corp');

2 objects will be created, which is equivalent to executing the following code:

system =  {} ;
system.corp =  {} ;

In this way, when we customize the class, we can use it like this:

Ext .namespace('system.corp');

system.corp.ManageCorp = function()  {
    //dosomething
}

If you also want to define a class with the same name, you can use different namespaces to distinguish, so that the two classes will not conflict:

Ext .namespace('system.admin');

system.admin.ManageCorp = function()  {
    //dosomething
}

In addition, pay attention to the use of "eval" method in the source code, if necessary, you can use this method to solve the problem.

2.

For packages in java, we all think that it is mainly a method of managing classes introduced for the repetition of class names. Similarly, because js defines too many classes, it is inevitable that duplicate class names will be defined. Especially in today's world where projects need to be done collaboratively. Ext .namespace was born for this.

Ext .namespace('com.company');
is to define a package of com.company.

com.company.ClassA = function(){}; is to define a Class under the com.company package. Now that the package is defined, why write Ext .namespace(' com.company')? Can't write com.company.ClassA = function(){} directly? Isn't com.company.ClassA just a variable name? Where is the bag from. After practice, the variable name of javascript is not allowed to include '.' . Therefore, directly writing com.company.ClassA = function(){}; is syntactically incorrect.

Check the official explanation of ext , if we don't use Ext .namespace, creating a namespace should be like this.

if (!App) App = {};
if (!App.form) App.form = {};
if (!App.data) App.data = {};

And  Ext .namespace('App', 'App.form', 'App.data');
has the same effect as the above three sentences.

It can be seen that this package is essentially an object, a window variable. That is, our package is a hierarchical object. For example, com.company should be interpreted as a property company of the com object. If you define a class under this package, you should add this class to the attribute company of the com object.

3.

Ext .namespace("webbook.author.comboBox")
is equivalent to creating three objects in JS: (similar to JAVA packages)
webbook={};
webbook.author={};
webbook.author.comboBox={};


Object assignment:
webbook=["t","v"];
webbook.author.comboBox=[["t1","v1"],["t2","v2"]];
webbook.author.comboBox. level=[....] //Define "object.variable" assignment, but not webbook.author.comboBox.level.name=[..], because level is not an object.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326830781&siteId=291194637