Closures, which define additional functions within a function, can be called by objects, and these functions can refer to the
Temporary variables in the body of the outer function, such as name, can be indirectly persisted, and only through these functions
To be used, the temporal variables have disappeared, but these functions can still be referenced.
Closures can cause garbage collection problems and are poorly performing to discard. Use prototypes instead.
function person (name) {
var _firstname=name;//private variable
this.name=name;//Public variables
This.sayhello=function () {
alert (this.name);
}
}
var billgates=new person ("billgates");
alert (billgates.name);//billgates, can access
Alert (billgates._firstname+ "name");//undefined, private variable cannot be accessed
Billgates.sayhello ()//Even if the parameter name is extinct, it can be obtained by referencing the name of the person.
The temporary variable is implicitly preserved, and dies with the billgates.
JS Closed Package