Five ways to create a namespace using JavaScript _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Five methods of writing JavaScript to create a namespace. In JavaScript, global variables often cause naming conflicts. sometimes, you may not rewrite the variables in the order you want. you can refer to the following example: copy the code. in JavaScript, global variables often cause naming conflicts. sometimes, variable rewriting is not in the order you want. you can refer to the following example:

The code is as follows:


Var sayHello = function (){
Return 'Hello var ';
};

Function sayHello (name ){
Return 'Hello function ';
};

SayHello ();


The final output is

The code is as follows:


> "Hello var"


Why? according to StackOverFlow, JavaScript is parsed in the following order.

The code is as follows:


Function sayHello (name ){
Return 'Hello function ';
};

Var sayHello = function (){
Return 'Hello var ';
};

SayHello ();


The function declaration without var is parsed in advance. Therefore, we recommend that you always use the pre-var statement to declare all variables in modern JS writing;

The best way to avoid global variable name conflicts is to create a namespace. below are several common methods for creating a namespace in JavaScript.

1. create a function

This is a common method. it is implemented by declaring a function, setting the initial variable in the function, and writing the common method to prototype, for example:

The code is as follows:


Var NameSpace = 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 method is too long to compress the code (jQuery uses fn to replace prototype) and needs to be instantiated (new) before calling ). You can write data in JSON format using Object:

2. create an Object through a JSON Object

The code is as follows:


/*
Object
*/
Var NameSpace = NameSpace || {};
NameSpace. Hello = {
Name: 'World'
, SayHello: function (_ name ){
Return 'hello' + (_ name | this. name );
}
};


Call

The code is as follows:


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

This method is relatively compact. The disadvantage is that all variables must be declared as public. as a result, this indicates the scope of all references to these variables, and the writing is slightly redundant.

3. implemented through Closure and Object

Declare all variables and methods in the closure, and return the public interface through a JSON Object:

The code is as follows:


Var NameSpace = NameSpace || {};
NameSpace. Hello = (function (){
// Public object to be returned
Var self = {};
// Private variables or methods
Var name = 'world ';
// Public method or variable
Self. sayHello = function (_ name ){
Return 'hello' + (_ name | name );
};
// Return public object
Return self;
}());

IV. improved writing of objects and closures

In the previous example, you also need to add self to the internal call to public methods, such as self. sayHello (); here, you can finally return the JSON object of all public interfaces (methods/variables.

The code is as follows:


Var NameSpace = NameSpace || {};
NameSpace. Hello = (function (){
Var name = 'world ';
Var sayHello = function (_ name ){
Return 'hello' + (_ name | name );
};
Return {
SayHello: sayHello
};
}());

V. simple Function writing

This is a simple implementation with a compact structure. it uses function instances and does not need to be instantiated (new) during Calling. The solution comes from stackoverflow:

The code is as follows:


Var NameSpace = NameSpace || {};
NameSpace. Hello = new function (){
Var self = this;
Var name = 'world ';
Self. sayHello = function (_ name ){
Return 'hello' + (_ name | name );
};
};


Please add.

Pipeline code such...

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.