JS closure method example analysis

This example tells the JS closing method. Share to everyone for reference, specifically:

      // First, the function is used as the return value function fn () {var max = 10; returnif (x) {if (x> max) {console.log (x);}};} var f1 = fn (); F1 (15);   // Second, the function is transmitted as the parameter is transmitted VAR MAX = 10; fn = function (x) {if (x> max) {Console.log (x); // 15}}; (function (f) {var max = 100; f (15);}) (fn);   function fn () {var max = 10; return function bar (x) {IF (if> max) {console.log (x);}};} var f1 = fn (); max = 100; F1(15);      The first step , the code execution generates a global context environment, and is executed Assign the variables. At this time, the global context environment is active.   Global context environment: max is undefined  
, perform VAR F1 = Fn (); when code is called, fn (), generate Fn () execution Context environment, stack, and set to active status.
Fn () context environment: max is 10

, the third step , executed VAR F1 = Fn (); and fn () call completion. By reasonable that the FN () execution context should be sold, but this cannot be done here.

Note that the focus is coming: When the Fn () is executed, it is a function. The particular point of the function is that you can create a separate scope.

is coincidentally, in the returned this function body, there is a free variable max to reference the Max in the FN () context environment in the FN scope.

Therefore, this max cannot be destroyed, and the max of the BAR function will not be found after destroying. Therefore, the FN () context environment here cannot be destroyed, and still exists in the context stack.

Performed to max = 100; when the global context environment will become active, but the fn () context environment will remain in the context stam.

In addition, MAX = 100 is performed; the max is assigned to 100 in the global context environment.

Global context environment: max is 100 fn () context environment: max is 10

Fourth step

, execute to F1 (15);, execute F1 (15 ), The BAR (15) is executed, and the BAR (15) context environment is created, and it is set to active.

Perform BAR (15) When max is a free variable, you need to find it to the scope of the BAR function, find the value of MAX 10.

Here, the creating BAR function is created when performing Fn (). Fn () has long been executed, but the fn () executing the context environment also exists in the stack, so the BAR (15) can find it. If the fn () context environment is destroyed, then MAX can not be found.

Use closures will increase content overhead, now it is obvious!

Fifth step

, executed F1 (15); is the destruction process of the context environment, here is not described again.

Closet and scope, the context environment has a secret relationship, really “I want to say that you are not easy”!

In addition, the application in jQuery is very much, no matter what you want to know a classic framework / class library, you still want to develop a plug-in or class library, like closed bag, prototype these basic theories It is necessary to know. Otherwise, you don’t know why, you can’t know why, because these bugs may be except your knowledge.

More about JavaScript Related Contents View this Topic: “JavaScript Object-Oriented Telings”, “JavaScript JSON Operation Skills Summary”, “JavaScript Switch Special Effects and Skills Summary”, “JavaScript Find Algorithm Skills Summary “JavaScript error and debugging skills summary”, “JavaScript data structure and algorithm skill summary”, “JavaScript overhead algorithm and skill summary” and “summary of JavaScript mathematical computing method”

I hope this article will be held to everyone JavaScript Program design helps.

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment