Sunday 16 October 2016

javascript - When actually is a closure created?

Is it true that a closure is created in the following cases for foo, but not for bar?



Case 1:






foo is a closure with a scope chain with only the global scope.



Case 2:






same as Case 1.



Case 3:








in this case, Circle.prototype.foo (which returns the circle's area) refers to a closure with only the global scope. (this closure is created).



Case 4:






here, foo is a closure with only the global scope, but bar is not a closure (yet), because the function foo is not invoked in the code, so no closure bar is ever created. It will only exist if foo is invoked , and the closure bar will exist until foo returns, and the closure bar will then be garbage collected, since there is no reference to it at all anywhere.



So when the function doesn't exist, can't be invoked, can't be referenced, then the closure doesn't exist yet (never created yet). Only when the function can be invoked or can be referenced, then the closure is actually created?

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...