The last two sections talked about JavaScript object-oriented namespace and javascript object-oriented JavaScript class. You can read the above first and then continue reading.
It’s actually very simple, don’t talk nonsense, I believe you will understand it at a glance after reading the code and comments below!
//Declare a class, which is a method, In fact, in JavaScript, namespaces, classes, members... everything is an object
MyClass =function(){
var _this=this;
//Private variable
var aa="11 ";
//Public variable
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 nutshell: use var inside the class
Variables or methods declared with the keyword are private;
Methods declared with the function keyword are private;
Variables or methods declared with the this keyword are public.
The above are all for instance classes, but for static classes it is even simpler. JavaScript static classes are actually a json object, so all its members are public. It is visible to the outside world!