In the javascript--closure section We explain the characteristics of the scope and scope chain of closures. It is understood that it is generally impossible to access variables in the internal scope outside, but through closures we can define privileged methods to access private variables. The following first describes the block-level scope and introduces several privileged methods.
I. Impersonation of block-level scopes
JavaScript is not a block-scoped concept. So the variables we define in the statement block are scoped to contain the function rather than the statement block.
function OutPut (count) { for (var j=0;j<count;j++) { alert (j); } Alert (j); // Count }
In Java, C + + statements, the variable j is destroyed after the loop statement ends. But in JavaScript, the variable j is the variable for the function output. So we use the variable of the function expression to imitate the block-level scope.
function OutPut (count) { (function () { for(var j=0;j<count;j++ ) { alert (j); } }) (); Alert (j); // error }
At this time, the variable j is only valid in the private scope and is destroyed at the end of the run. This technique is often used outside the function in the global scope to limit the addition of too many variables and functions to the global environment.
Second, the method of accessing the constructor function
Now let's introduce the first method of accessing private variables-constructor method. Private variables are variables, parameters, and other functions that are defined inside a function.
function Person (name) { var name=name; this. sayname=function() { alert (name); } }; var person1=New person ("Bob"); var person2=New person ("Mike"); Person1.sayname (); // Bob Person2.sayname (); // Mike
The Sayname () method is a privileged method for all instances of a person. But there is a problem:
alert (person1.sayname==person2.sayname); // false
The privileged method with the same name for each instance is recreated.
Iii. Borrowing prototype access
function Person (name) { var name=name; Person.prototype.sayName=function() { alert (name); } };
var person1=New person ("Bob"); Person1.sayname (); // Bob var person2=New person ("Mike"); Person1.sayname (); // Mike Person2.sayname (); // Mike alert (person1.sayname==person2.sayname); //true
The privileged methods for each instance of this method are dynamically shared. However, each instance does not have its own private variable.
Iv. Combination of Access
function Person (name) { var name=name; Person.prototype.sayName=function() { alert (name); }; this. sayprivatename=function() { alert (name); } };
Combine the two ways to see the effect of accessing a private variable:
varperson1=NewPerson ("Bob"); Person1.sayname (); //BobPerson1.sayprivatename ();//Bob varPerson2=NewPerson ("Mike"); Person1.sayname (); //MikePerson1.sayprivatename ();//BobPerson2.sayname ();//Mikealert (person1.sayname==person2.sayname);//truealert (person1.sayprivatename==person2.sayprivatename);//false
Private variables are dynamically shared if they are accessed by a prototype method, and private variables are specific in each instance if accessed through an instance method. Unlike instance properties, instance properties override the same-name prototype attribute and are always specific.
Five, module mode
The preceding method creates a privileged method for a custom type, while the module mode creates a privileged method for a singleton.
varperson=function(){ varName= "Bob";//Private Variables functionPrivatefunction () {//Private FunctionsAlerttrue); }; return{publicname:name,//Privileged PropertiesPublicmethod:function(){//Privileged Methods returnprivatefunction (); } } }();
alert (person.publicname); // Bob Person.publicmethod (); // true
In short, you can use module mode if you have to create an object and initialize it, and also expose some methods that can access those private variables.
javascript--Privileged Methods