Use of senchatouch namespace Ext. NS ()

Source: Internet
Author: User

Senchatouch uses a namespace similar to a Java package to hide something. This prevents all variables from being changed to global variables.

If there is no namespace, either you write a lot in the function object, so that the global variables will be much less of the global variables, but when you do a large project, thatCodeIt's not clear. Using a namespace can reduce the declaration of global variables so that it does not affect the variables in other objects.

However, when you name a variable, you 'd better use the VaR keyword. If you do not declare the variable with the VaR keyword, your variable will be the global variable under the window by default.

However, the use of closures can also play an effect, but I have never tried it. It is such a function declaration (function (){})();

The following describes how to use a namespace. Ext. NS () is short for Ext. namespace () and is used to declare a namespace.

In fact, just getting familiar with the Javascript syntax knows that there is no such thing at all. In fact, ext only simulates the namespace (or package) by using similar usage of attributes added to the object ). Because the object or method is a closure, the variables defined in it are closed to the outside world.

Ext. NS ('myspace '); // It is equivalent to defining an empty function object (or think it is a class) and can be written as MySpace = ext. emtyfn (); or MySpace = {}

MySpace. APP = ext. Panel ({}); // This MySpace. app adds an attribute to the MySpace object and the attribute name is app (it is a function object of the Panel ).

If Ext. NS ('myspace ', 'myspace. data', 'myspace. views '); // you define three objects, MySpace, and MySpace data. The views attribute is an object, so you can access MySpace. data. moumou to add the attributes of this object.

Ext. another feature of NS () is Ext. NS ('myspace. data. box '); with ext. NS ('myspace ', 'myspace. data', 'myspace. data. box ') is the same. In addition, if you define a namespace, if you put it in the first JS, everything in the namespace will be valid before you, no, but declared and not defined. Because before that, your attributes are already in front (as mentioned in the namespace, if used, they are assigned a value before the namespace ).

Using namespaces, you can divide views, data, and class into different JS files and place them in different folders. This makes the code clearer and more clean, and facilitates maintenance and changes.

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.