Analysis of Ext. namespace

Source: Internet
Author: User

I thought it would be good to use ext3.x when I was working on a project and did not have a deep understanding of it. Sencha has encountered this problem recently. So I searched a lot of related information online, so I would like to summarize it here.

1.

Ext. namespace method, which is used to convert input parameters to objects. the main purpose of this method is to partition classes with the same name, which is similar to the package in Java. let's take a 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, obtain the parameters of the namespace method through arguments, divide them into arrays by dots, and recursively assign values to empty objects. The namespace built in the system is used.

The Code shows that if the input string parameter is separated by ".", multiple objects will be created, for example:

Ext. namespace ('System. Corp ');

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

System = {};
System. Corp = {};

In this way, we can use the following when creating a custom class:

Ext. namespace ('System. Corp ');

System. Corp. managecorp = function (){
// Dosomething
}

If you want to define a class with the same name, you can use different namespaces to differentiate them 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 the "eval" method in the source code. If necessary, you can use this method to solve the problem.

2.

For Java packages, we all think that it is mainly a management method introduced for repeated class names. Similarly, because JavaScript defines too many classes, it is inevitable that duplicate class names will be defined. Especially in today's world where project collaboration is required. Ext. namespace is born for this purpose.

Ext. namespace ('com. company ');
Is to define a com. Company package.

Com. Company. classa = function () {}; defines a class under the com. Company package. Since the package is defined, why do we need to write Ext. namespace ('com. company ')? Can I directly write com. Company. classa = function? Com. Company. classa is not a variable name? Where is the package. After practice, the variable names in Javascript do not include. Therefore, writing com. Company. classa = function () {} directly is incorrect in syntax.

Check the official explanation of Ext. If Ext. namespace is not used, create a namespace as follows.

If (! APP) app = {};
If (! App. Form) app. Form = {};
If (! App. Data) app. Data = {};

Ext. namespace ('app', 'app. Form', 'app. data ');
The role of the above three statements is the same.

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

3.

Ext. namespace ("webbook. Author. ComboBox ")
JS creates three objects: (similar to Java package)
Webbook = {};
Webbook. Author = {};
Webbook. Author. ComboBox = {};

Image assignment:
Webbook = ["T", "V"];
Webbook. Author. ComboBox = [["T1", "V1"], ["T2", "V2"];
Webbook. author. comboBox. level = [...] // define "object. variable "value assignment, but not webbook. author. comboBox. level. name = [...], because level is not an object.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.