The last two sections talk about the JavaScript object-oriented namespace and the javascript object-oriented JavaScript class. You can read the above before proceeding.
In fact, it is very simple. I believe you will be clear at a glance after reading the following code and comments!
Copy codeThe Code is as follows:
// Declaration class is a method. In JavaScript, namespace, class, and member... everything is an object.
MyClass = function (){
Var _ this = this;
// Private variable
Var aa = "11 ";
// Publish Variables
This. bb = "22 ";
// Private Method
Function fun1 (){
Alert (aa );
Alert (_ this. bb );
}
// Private Method
Var fun2 = 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
Variables or methods declared with the var keyword are private;
The method declared with the function keyword is private;
Variables or methods declared using the this 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!