Imitating block-level scopes _ JavaScript skills for javascript anonymous functions

Source: Internet
Author: User
This article describes how JavaScript anonymous functions mimic block-level scopes. For more information, see Anonymous functions

Functions are the most flexible objects in JavaScript. here we will only explain the usage of anonymous functions.

Anonymous function: a function without a function name.

Function definition: first, briefly introduce the definition of a function, which can be roughly divided into three methods.

First: this is also the most common one.

function double(x){  return 2 * x;  }

Second: This method uses the Function constructor and uses the parameter list and Function bodies as strings. this method is inconvenient and is not recommended.

var double = new Function('x', 'return 2 * x;');

Third:

var double = function(x) { return 2* x; }

Note that the function on the right of "=" is an anonymous function. after the function is created, the function is assigned to the variable square.

JavaScript does not have block-level scope. That is to say, the variables defined in block-level statements are actually created in include functions (external functions) rather than statements.

function outputNumber(count){   for(var i=0;i<1000;i++){     alert(i);   }   alert(i);  //count } 

In java, C #, and other languages, variable I is defined only in the for loop statement. when the loop ends, the I is destroyed. However, in JavaScript, variable I is defined in the outputNumber () activity object, so it can be accessed inside the function at the beginning of its definition. Even if the same variable is declared again, its value will not be changed.

Function outputNumber (count) {for (var I = 0; I <1000; I ++) {alert (I) ;}var I; // re-declare the variable alert (I); // count}

Anonymous functions can be used to simulate block-level scopes and avoid this problem. The syntax of anonymous functions used as block-level scopes (also called private scopes) is as follows:

(Function () {// This is block-level scope })()

The code definition mentioned above uses an anonymous function to include the function declaration in a parentheses, indicating that it is a function expression. The next pair of parentheses immediately calls this function.
Whenever you need some variables temporarily, you can use private scopes, for example:

Function outputNumber (count) {(function () {for (var I = 0; I <1000; I ++) {alert (I );})(); alert (I); // cause an error}

In this way, a private scope is inserted outside the for loop. Any variables defined in anonymous functions will be destroyed at the end of execution.

This technique is often used outside the function in the global scope to restrict the addition of excessive variables and functions to the global scope.

In general, we should try to minimize the number of variables and functions added to the global scope.

This method can reduce the memory usage of the closure, because there is no reference to the anonymous function, as long as the function execution is complete, its scope chain can be destroyed immediately.

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.