Thoughts on namespaces in JavaScript programming ideas

Source: Internet
Author: User
Document directory
  • Updated on 2010.1.29

JavaScript programming ideas, thisProgramming IdeologyA great word is really tangled in my heart. The javascript namespace information is searched on the Internet. I just want to make a summary.

Common Code on the internet is too common, because it is convenient and simple:

var GLOBAL = {    str: 'linkClass',    init: function(){        return str;    }};

The above is also the classic code that I use when I receive guidance from the front-end cool. I just came out of the classic network some time ago.

Don't you want to stick to it? Next step. However, let's look at the equivalent code before the next step:

var GLOBAL = {};GLOBAL.str='linkClass';GLOBAL.init=function(){    return str;};

Er, As long as JavaScript is written, it will all be written above (look at this buddy's writing ). The next step is actually equivalent code:

var GLOBAL = {};GLOBAL['str']='linkClass';GLOBAL['init']=function(){    return str;};

The problem is that the attributes and methods in the above objects have not been loosely coupled, and the most basic object-orientedEncapsulationFeatures. You don't need to expose the STR attribute, because the init method is used to return the STR value. Therefore:

(function(){var str='linkClass';function init(){    return str;}window['GLOBAL']={};window['GLOBAL']['init']=init;})();

In this example, the attributes or methods defined in the namespace are global, that is, the interfaces exposed, private variables cannot get or set outside the Javascript closure. STR is retrieved from the init function and then found along the scope chain. It also achieves loose coupling.

Loose couplingI have seen this concept in the "idea" of the Yahoo front-end from the library to the framework.

Therefore, I was shocked after learning from the source of this Code. The above explanation is quite common. The following is an excerpt from section 8.8.2 In the Javascript authoritative guide (version 5th:

It is sometimes useful to define a function simply to create a call object that acts as a temporary namespace in which you can define variables and create properties without corrupting the global namespace. suppose, for example, you have a file of JavaScript code that you want to use in a number of different javascript programs (or, for client-side JavaScript, on a number of different web pages ). assume that this code, like most Code, defines variables to store the intermediate results of its computation. the problem is that since this code will be used in your different programs, you don't know whether the variables it creates will conflict with variables used by the programs that import it.

The solution, of course, is to put the code into a function and then invoke the function. This way, variables are defined in the call object of the function:

function init() {    // Code goes here.    // Any variables declared become properties of the call    // object instead of cluttering up the global namespace.}init();  // But don't forget to invoke the function!

The Code adds only a single property to the global namespace: The property "init", which refers to the function. if defining even a single property is too much, you can define and invoke an anonymous function in a single expression. the code for this JavaScript idiom looks like this:

(function() {  // This function has no name.    // Code goes here.    // Any variables declared become properties of the call    // object instead of cluttering up the global namespace.})();          // end the function literal and invoke it now.

Note that the parentheses around the function literal are required by JavaScript syntax.

According to Hax, ie has a global variable did (personality classification ). Therefore, use it with caution! In addition, this is not the so-called "JS layering concept ".

Updated on 2010.1.29

Another method is to use a function as the namespace. For more information, see David's original article functions as namespaces, and how to peek inside and the translation "how to access the internal variables of a function as a namespace".

Related Article

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.