The use of return and closure in Javascript are described in detail, and javascriptreturn

Source: Internet
Author: User

The use of return and closure in Javascript are described in detail, and javascriptreturn

Preface

Closure in Javascript is an expression (usually a function) with many variables and an environment bound to these variables. Therefore, these variables are part of the expression. The return Statement plays an important role in js. This keyword not only has the function of returning functions, but also has some special usage, the following describes the use of return and closure in Javascript.

I. Use of return

Case 1:

Var a = 1; for (var B = 0; B <10; B ++) {return B ;}; sonsole. log (B) // The returned result is null.

I personally think that both the left and right are global, and the code will not be executed after return;

Case 2:

Var a = 1; function bb () {for (var B = 0; B <10; B ++) {return B ;}}; console. log (bb); // return 0

Ii. Chain scope and Closure

Let's first look at a case:

var a=1;function f1(){ var b=2; function f2(){ console.log(a);//1 console.log(b)//2 };};

Chain scope:In the code above, function f2 is included in function f1. All local variables in function f1 are visible to f2. But in turn, the local variables in f2 are invisible to f1. This is the unique "chain scope" structure of Javascript;

Closure:To put it bluntly, you can use one method to access local variables;

In Javascript, only the subfunctions in a function can read local variables. Therefore, you can simply understand the closure as a function defined in a function ".

Therefore, in essence, closure is a bridge connecting the internal and external functions of a function.

Function f1 () {var a = 1; function f2 () {alert (a) ;}; return f2}; console. log (f1 (); // function f2 () {alert (a) ;}; console. log (f1 (); // 1 is displayed.

Therefore, you can write as follows:

Function f1 () {var a = 1; function f2 () {alert (a) ;}; return f2}; var result = f1 (); console. log (result (); // 1 is displayed

Complete closure case:

function f1(){ n=999; function f2(){   alert(n);  }    return f2;}var result=f1();result(); // 999

Now we can use f2 to access the local variable n in f1, which is the closure.

Closure: one is to read the internal variables of the function mentioned above, and the other is to keep the values of these variables in the memory.

Function f1 () {var a = 222; f2 = function () {// note that this is written here. If var is used, it is a local variable. If it is not applicable, it is a global principle; alert (a) ;};}; f1 () // null f2 () // 222. function f1 () {var a = 222 can be accessed without a closure; function f2 () {// The alert (a) ;}; nAdd = function () {a + = 1}; return f2}; var obj = f1 (); obj () // 222 nAdd () // empty obj (); // 223, indicating that local variable a in function f1 is always stored in the memory, not Automatically cleared after f1 call

Why? The reason is that f1 is the parent function of f2, and f2 is assigned a global variable, which causes f2 to always be in the memory, while f2 depends on f1, therefore, f1 is always in the memory and will not be recycled by the garbage collection mechanism after the call is completed.

Another noteworthy part of this code is"nAdd=function(){n+=1}"This line does not use the var keyword before nAdd, so nAdd is a global variable rather than a local variable. Secondly, the value of nAdd is an anonymous function.

The anonymous function itself is also a closure, so nAdd is equivalent to a setter. You can operate on local variables inside the function outside the function.

Notes for using closures

1) because the closure will make the variables in the function be stored in the memory, the memory consumption is very large, so the closure cannot be abused, otherwise it will cause the performance of the web page, memory leakage may occur in IE. The solution is to delete all unused local variables before exiting the function.

2) The closure changes the value of the internal variable of the parent function outside the parent function. Therefore, if you use the parent function as an object, use the closure as its Public Method, and use internal variables as its private value ), be sure to be careful.

Summary

The above is all about this article. I hope this article will help you in your study or work. If you have any questions, please leave a message.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.