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:Copy codeThe Code is as follows: // declare a Global Object registernamespace function. The parameter is the full path of the namespace, such as "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 "......Copy codeThe Code is as follows: registernamespace ("cnblogs. blog ");
Registernamespace ("cnblogs. News ");
Registernamespace ("cnblogs. Group ");
In fact, the namespace here is an object, an object.
Author: cnblogs xiangshu