Several ways to create namespaces in JavaScript __java

Source: Internet
Author: User
Tags closure compact
Global variables often cause naming conflicts in JavaScript, and even sometimes rewrite variables are not in the order you imagined, but look at the following example:

var SayHello = function () {return
  ' Hello var ';
};

function SayHello (name) {return
  ' Hello function ';
};

SayHello ();

The final output is
> "Hello var"

Why, according to StackOverflow's interpretation, JavaScript is actually parsed in the following order.

function SayHello (name) {return
  ' Hello function ';
};

var SayHello = function () {return
  ' Hello var ';
};

SayHello ();

function declarations without var are resolved in advance, so modern JS notation suggests that you always declare all variables using the preceding VAR;
The best way to avoid global variable name collisions is to create namespaces, which are some of the common ways to build namespaces in JS.

created by functions (function)
This is a more common way of writing, by declaring a function implementation, which sets the initial variable, and the public method writes prototype, such as:
var NameSpace = window. NameSpace | | {};
/*
function
/Namespace.hello = function () {
this.name = ' world ';
};
NameSpace.Hello.prototype.sayHello = function (_name) {return
' Hello ' + (_name | | this.name);
var hello = new Namespace.hello ();
Hello.sayhello ();

This is a lengthy formulation that is not conducive to compressed code (jquery uses FN instead of prototype) and requires instantiation (new) before calling. Using object as JSON can be written in a more compact way:
creating an object from a JSON object
/*
Object
*/
var NameSpace = window. NameSpace | | {};
Namespace.hello = {
name: ' World '
, Sayhello:function (_name) {return
' Hello ' + (_name | | this.nam  e);
}
};

Call
NameSpace.Hello.sayHello (' JS ');
> Hello JS;

This kind of writing is compact, the disadvantage is that all variables must be declared publicly (public), resulting in all references to these variables need to add this to the scope, the wording is slightly redundant.
Implemented by closures (Closure) and object
Declare all variables and methods in the closure and return the public interface via a JSON object:
var NameSpace = window. NameSpace | | {};
Namespace.hello = (function () {
//The public object to be returned
var self = {};
The private variable or method
var name = ' World ';
Public method or variable
Self.sayhello = function (_name) {return
' Hello ' + (_name | | name);
};
The returned public object return
self;
} ());

improved type writing of object and closure
The previous example of an internal call to the public method also requires the addition of self, such as: Self.sayhello (), where the JSON object of all public interfaces (methods/variables) can be returned last.
var NameSpace = window. NameSpace | | {};
Namespace.hello = (function () {
var name = ' World ';
var SayHello = function (_name) {return
' Hello ' + (_name | | name);
};
return {
Sayhello:sayhello
};
} ());

concise wording of function
This is a relatively simple implementation, compact, through the function instance, and calls without instantiating (new), the scheme from StackOverflow:
var NameSpace = window. NameSpace | | {};
Namespace.hello = new function () {
var self = this;
var name = ' World ';
Self.sayhello = function (_name) {return
' Hello ' + (_name | | name);
};
};

Call

NameSpace.Hello.sayHello ();

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.