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 name conflicts, making management and maintenance troublesome. In this case, we need to use the object-oriented idea to develop JavaScript. Let's do this:
This section describes the private and public members of JavaScript. Although JavaScript does not have private and public keywords, it is still the same sentence-as developers, we need to have an object-oriented idea!
In fact, it is very simple. I have read the followingCodeAnd comments are clear to you!
// A declarative class is a method. In JavaScript, namespaces, classes, and members... all objects are objects.
Myclass = Function (){
VaR _ This = This ;
//Private variable
VaRAA="11";
//Public Variables
This. Bb="22";
//Private Method
FunctionFun1 (){
Alert (AA );
Alert (_ this. bb );
}
//Private Method
VaRFun2=Function(){
Alert (AA );
Alert (_ this. bb );
}
//Public Method
This. Fun3=Function(){
Alert (AA );
Alert (_ this. bb );
}
}
// The test is as follows:
VaR MC = New Myclass ();
MC. AA = " AA " ; // Error
MC. bb = " Bb " ; // Correct
MC. fun1 (); // Error
MC. fun2 (); // Error
MC. fun3 (); // Correct
In a word: Inside the class
UseVaRThe variables or methods declared by the keyword are private;
UseFunctionThe keyword declaration method is private;
UseThisThe variables or methods declared by the keyword are public.
All of the above are for instance classes, and static classes are simpler. Javascript static classes are actually a JSON object, so all its members are public, they are all externally visible!
Address: http://www.cnblogs.com/xumingxiang/archive/2010/04/30/1725364.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.