This article introduces the block-level scope, the private variable and the module mode in JavaScript in detail, and the nonsense is not much said, as follows:
1. Block-level scopes (private scopes) are often used outside of functions in global scope, limiting the addition of too many variables and functions to the global scope.
(function (count) {for
(var i=0;i<count;i++) {
console.log (i);//=>0, 1, 2, 3, 4
}
console.log (i) ;//=>5
(function () {
var now=new Date ();
if (Now.getmonth () ==0 && now.getdate () ==1) {
console.log ("Happy New Year");
} else{
Console.log ("to the fullest expectations");
}
2. Private variable: any variable defined in a function can be considered a private variable because it cannot be accessed outside the function.
Privileged methods: Public methods that have access to private variables and private functions are called privileged methods.
2.1 Define the privileged method in the constructor:
function person (name) {
this.getname=function () {return
name;
};
This.setname=function (value) {
name=value;}
;
}
var person1=new person ("Jason");
Console.log (Person1.getname ());//=>jason
person1.setname ("Gray")
; Console.log (Person1.getname ());//=>gray
var person2=new person ("Michael")
; Console.log (Person1.getname ());//=>gray
Console.log (Person2.getname ());//=>michael
Person2.setname (' Alex ');
Console.log (Person1.getname ());//=>gray
The disadvantage of a constructor pattern is that you create the same set of new methods for each instance.
2.2 Static private variables to implement privileged methods
In a private scope, you first define a private variable and a private function, and then define the constructor and its public methods.
(function () {
//private variable and function
var name= "";
Person=function (value) {
name=value;
};
Privileged method
Person.prototype.getname=function () {return
name;
};
Person.prototype.setname=function (value) {
name=value;
}
}) ();
var person1=new person ("Jason");
Console.log (Person1.getname ());//=>jason
person1.setname ("Gray")
; Console.log (Person1.getname ());//=>gray
var person2=new person ("Michael")
; Console.log (Person1.getname ());//=>michael
Console.log (Person2.getname ());//=>michael
Person2.setname (' Alex ');
Console.log (Person1.getname ());//=>alex
Console.log (Person2.getname ());//=>alex
3. Module mode: It can be enhanced by adding private variables and privileged methods for a single instance.
You can use module mode if you have to create an object and initialize it with some data, and expose some ways to access the private data.
var application=function () {
//private variable and function
var components=[];
Initialize
Components.push (new Basecomponent ());
Public interface return
{
getcomponentcount:function () {return
components.length;
},
Registercomponent:function () {
if (typeof component== "Object") {
Components.push (component);
}
}
}
} ();
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.