Object-oriented JavaScript (1): namespace

Source: Internet
Author: User
For Javascript in small projects, you only need to write a few functions. However, in large projects, especially in developing websites that require good user experience, such as SNS, a large amount of javascrpt will be used. Sometimes the Javascript workload is better than C #, at this time, writing a bunch of functions will become messy, messy, and even cause name conflicts, making management and maintenance quite troublesome. In this case, we need to use the object-oriented idea to develop JavaScript. Let's do this:

For a project, you must first have a namespace. So the first thing we need to do is define a function to register a namespace.CodeAs follows:

// Declares a Global Object registernamespace function. The parameter is the full path of the namespace, for example, "cnblogs. blog"
Registernamespace =   Function (Fullname ){
// Cut the namespace into N parts
VaR Nsarray = Fullname. Split ( ' . ' );
VaR Streval =   "" ;
VaR Strns =   "" ;
For ( VaR I =   0 ; I < Nsarray. length; I ++ ){
If (I ! =   0 ){
Strns + =   " . " ;
}
Strns + = Nsarray [I];
// Create statements to construct namespace objects in sequence (if they do not exist)
Streval + =   " If (typeof ( "   + Strns +   " ) = 'Undefined ') "   + Strns +   " = New object (); "
}
If (Streval ! =   "" ) Eval (streval );
}

Now let's register several namespaces. Let's take the blog Park as an example. There are several modules "blog", "news", and "group" in the blog Park "......

Registernamespace ( " Cnblogs. Blog " );
Registernamespace ( " Cnblogs. News " );
Registernamespace ( " Cnblogs. Group " );

In fact, the namespace here is an object, an object.

If you write a bunch of functions, it is inevitable that there will be a variable or function name conflict. The namespace is applied and the code is easy to manage.


Address: http://www.cnblogs.com/xumingxiang/archive/2010/04/30/1725148.html

Author: Xu mingxiang
Source: http://www.cnblogs.com/xumingxiang
Copyright: The copyright of this article is shared by the author and the blog
Reprinted: you are welcome to reprinted. To save the author's Creative Enthusiasm, please [reprinted] As required. Thank you.
Requirement: This statement must be retained without the author's consent; Article The original text connection is provided; otherwise, the legal liability is required.

 

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.