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.