The so-called closure should refer to: an internal function that reads variables other than the current function, that is, the context environment at which it was created.
The code copy is as follows:
function hello(){
var char = "hello,world";
function print(){
console.log(char);
};
return print();
}
It should be noted that the print function here refers to the char variable of the external hello function, so here we can return a
The code copy is as follows:
hello,world
In a sense, this function should be attributed to scope. Of course, we have no way to access char directly, unless there is an error when we declare this variable. like
The code copy is as follows:
function hello(){
char = "hello,world";
function print(){
console.log(char);
};
return print();
}
Just because there is a var missing.
The code copy is as follows:
Here hello becomes a closure. A closure is a special object. It consists of two parts: a function, and the environment in which the function is created. The environment consists of any local variables in the scope when the closure is created.
Javascript closures and this
It should be noted that problems may occur when reading this and arguments.
The code copy is as follows:
function hello(){
this.char = "hello,world";
function output(){
char = "I'm no hello world";
console.log(this.char);
};
return output();
}
Of course, this example is not appropriate enough. So, we need an additional example to explain this problem. The following is an example from "Javascript Advanced Programming" to illustrate this problem.
The code copy is as follows:
var name = "The window";
var object = {
name: "My Object",
getNameFunc: function(){
return function(){
return this.name;
}
}
};
object.getNameFunc()()()
But this usage is really, and the solution is to save a temporary variable that, as mentioned in the previous article "Some Knowledge on this in Javascript".
The code copy is as follows:
var name = "The window";
var object = {
name: "My Object",
getNameFunc: function(){
var that = this;
return function(){
return that.name;
}
}
};
object.getNameFunc()()()
Javascript closures and read and write variables
It is worth noting that if we do not process our variables well, we can also modify them.
The code copy is as follows:
function hello(){
var char = "hello,world";
return{
set: function(string){
return char = string;
},
print: function(){
console.log(char)
}
}
}
var says = hello();
say.set('new hello,world')
say.print() // new hello world
Javascript closures and performance
Quote MDC
The code copy is as follows:
If closures are not required for certain special tasks, it is unwise to create functions in other functions without need, because closures have negative effects on script performance, including processing speed and memory consumption.
The article also mentioned.
The code copy is as follows:
For example, when creating a new object or class, methods should usually be associated with the object's prototype rather than defined in the object's constructor. The reason is that this will cause the method to be reassigned once every time the constructor is called (that is, for the creation of each object).