Before reading the article, please read this article of Nanyi teacher first http://www.ruanyifeng.com/blog/2009/08/learning_javascript_closures.html
Written by the predecessors, and written very well, there is no need to repeat it. Just add some of Mr. Ruan's article did not say. by the way.
Quote a word: "The closure is functions that return function" (the source has been forgotten)
Types of closures: cyclic closures, function closures
Closure Features: External access function internal values, function internal variables are not recycled
The this point of the closure function in the function closure type.
Consider the following code:
var name = "Window"; var object = { Name: "Object", getnamefunc:function () { return function () { return this.name; }; } }; var obj = { Name: "Object", GetNameFunc:object.getNameFunc () }; function foo () { return this.name; } Console.log (foo ());//window Console.log (Object.getnamefunc ()); Window Console.log (Obj.getnamefunc ());//object
The closure function cannot directly access the This object that contains his function because the this point of the two is not the same.
The This of the external function points to the object that called him, and the inner function's this point is not difficult to understand because it points to the global object.
Analogy with the following two function calls
Foo ();//window
Object.getnamefunc () (); Window
In fact, when calling Object.getnamefunc (), as if the global object returned a function, the returned function is no different from the Foo function.
When this returned function is called in the global, the this of the function naturally points to the global object as if it were called the Foo function.
When Object.getnamefunc () is inside obj, the returned closure function is the property of obj, when this of the closure function points to the Obj object.
The point of the closure function this in JavaScript closures