The last two sections talk about JavaScript object-oriented namespaces and JavaScript-oriented JavaScript classes, so you can look at the above and continue to look down.
In fact, it is very simple, less talk, read the following code and comments believe you will be at a glance!
Copy Code code as follows:
Declaring a class is a method, in fact, in JavaScript, namespaces, classes, members .... All objects
MyClass =function () {
var _this=this;
Private variable
var aa= "11";
Exposing variables
this.bb= "22";
Private method
function Fun1 () {
alert (AA);
alert (_THIS.BB);
}
Private method
var fun2=function () {
alert (AA);
alert (_THIS.BB);
}
Exposing methods
This.fun3=function () {
alert (AA);
alert (_THIS.BB);
}
}
The test is as follows:
var mc=new MyClass ();
Mc.aa= "AA";//Error
mc.bb= "BB";/Right
MC.FUN1 ();//Error
Mc.fun2 ();//Error
MC.FUN3 ()//correct
Word: In the interior of 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 with the This keyword are public.
All of the above is for instance classes, and for static classes that is simpler, JavaScript static class is actually a JSON object, so all its members are public, are externally visible!