Private variables
Private variables
Any variable defined in a function can be considered a private variable, because these variables cannot be accessed outside of the function.
Private variables include arguments to functions, local variables, and other functions defined in the function.
Privileged Methods : methods that have access to private variables and private functions.
There are two ways to create a privileged method on an object: One is to define the privileged method in the constructor.
1 functionMyObject () {2 //private variables and private functions3 varPrivatevariable = 10;4 functionprivatefunction () {5 return true;6 }7 //Privileged Methods8 This. Publicmethod =function() {9privatevariable++;Ten returnprivatefunction (); One }; A}
A privileged method can be defined in a constructor because the privileged method has access to all variables and functions defined in the constructor as a closure.
For this example, variable privatevariable and function privatefunction can only be accessed through privileged methods.
With private variables and privileged methods, you can hide data that should not be directly modified, such as:
1 functionPerson (name) {2 This. GetName =function() {3 returnname;4 }5 This. SetName =function(value) {6Name =value;7 }8 }9 varperson =NewPerson ("CC");TenAlert (Person.getname ());//"CC" OnePerson.setname ("VV"); AAlert (Person.getname ());//"VV"
Disadvantages of constructors: each new instance creates the same set of new methods.
Static private variables
You can also create privileged methods by defining private variables or functions in a private scope.
1(function() {2 //private variables and private functions3 varPrivatevariable = 10;4 functionprivatefunction () {5 return false;6 }7 //constructor Function8MyObject =function() {9 };Ten //Privileged Methods OneMyObject.prototype.publicMethod =function() { Aprivatevariable++; - returnprivatefunction (); - }; the})();
A function declaration can only be used to create a local function, where the function declaration is not used, and the VAR keyword is not used, in order to create a global variable to facilitate access outside of the function.
Initializing undeclared variables always creates a global variable. (in strict mode, initialization of undeclared variables can cause errors)
1(function() {2 varName = "";3person =function(value) {4Name =value;5 };6Person.prototype.getName =function() {7 returnname;8 };9Person.prototype.setName =function(value) {TenName =value; One }; A })(); - - varPerson1 =NewPerson ("CC"); theAlert (Person1.getname ());//"CC" - - varPerson2 =NewPerson ("VV"); -Alert (Person2.getname ());//"VV" +Alert (Person1.getname ());//"VV"
The variable name becomes a static property that is shared by all instances.
Static private variables increase code reuse because of the use of prototypes, but all instances do not have their own private variables.
Finding more than one level of a scope chain can affect the search speed to some extent. This is where the shortcomings of using closures or private variables are.
JS function (6)-Private variable