This article analyzes the private static variables of JavaScript object-oriented. Share to everyone for your reference, specific as follows:
As you know, the principle of a private instance variable is based on the scope.
The private instance variable is implemented within the JavaScript function with the var keyword and is valid only within the function.
In imitation of this, the solution to the private static variable is presented:
<script language= "javascript" type= "Text/javascript" >
var jsclass = (function () {
var privatestaticvariable = "private static variable";
var privatestaticmethod = function () {
alert ("Invoke private static Method");
return function () {
this.test1 = function () {return
privatestaticvariable;
}
This.test2 = function (obj) {
privatestaticvariable = obj;
}
This.test3 = function () {
privatestaticmethod ();
}
};}
) ();
var testObject1 = new Jsclass ();
var testObject2 = new Jsclass ();
Alert (Testobject1.test1 ());
Testobject1.test2 ("changed private static variable");
Alert (Testobject2.test1 ());
Testobject2.test3 ();
</script>
Note that instead of directly defining a JavaScript class, you use an anonymous function as a container for static variables and return a JavaScript class .
For more information on JavaScript-oriented object-related content readers can view the site topics : "JavaScript Object-oriented introductory tutorial"
I hope this article will help you with JavaScript programming.