If you have contact with other languages, you should be exposed to the concept of a private variable. The so-called private is not protected, can not modify and access, only through the API you give access to the interface and modification. But there is no private variable in JavaScript, and all members can be accessed and modified by subscript, such as:
Private variable
The code is as follows |
Copy Code |
var F = function () {};
F.prototype.name = "Zhangsan";
var a = new F ();
alert (a.name); Zhangsan
A.name = "Lisi";
alert (a.name); Lisi |
In JavaScript, the member variables of an instance are so easily modified to set the delete. That's a lot of work.
The code is as follows |
Copy Code |
var F = function () {};
Variable preceded by an underscore F.prototype._name = "Zhangsan";
var a = new F ();
Access is the same as normal access alert (a._name); Zhangsan
It can actually be easily modified. A._name = "Lisi";
alert (a._name); Lisi |
It seems very common to underline the position of a private variable. But this is only one of the default conventions, in fact, using JavaScript features, we can define private variables like this:
The code is as follows |
Copy Code |
(function (window) { //define private variable defaults var name = "Zhangsan"; var F = function () {}; //Accessing private variables f.prototype.getname = function () { & nbsp; return name; }; //update private variables f.prototype.setname = function (str) { name = str; }; window. f = f; }) (window); var a = new F (); A.getname (); //Zhangsan A.setname ("Lisi"); A.getname (); //Lisi |
This is also the most widely used approach.
private static Variables
The code is as follows |
Copy Code |
<script language= "javascript" type= "Text/javascript" > var Jsclass = (function () { var privatestaticvariable = "Nowamagic"; var privatestaticmethod = function () { Alert ("Nowamagic"); }; 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 ("Change Nowamagic"); Alert (Testobject2.test1 ()); Testobject2.test3 (); </script> |
dynamically generating private variable accessors
code is as follows |
copy code |
Creates a new user object that accepts an object with many properties as the parameter function User (properties) { ///traverses all properties of the object and ensures that it is scoped correctly for (var i in Properti ES) { (function (which) { var p=i; //Create a new reader (getter) for this property which[' Get ' +p]=function () { return properties[p]; }; //Create a new setter for this property which["Set" +p]=function (val) { Properties[p]=val; }; }) (this); } ///Create a new instance of the user object and pass an object with two properties to the parameter Var user=new user ({name: "Bob", age:44}); //Read property value Alert (User.getname ()); //Set property value User.setage; |