JavaScript anonymous functions mimic block-level scope _javascript techniques

Source: Internet
Author: User

anonymous functions

A function is one of the most flexible objects in JavaScript, and it simply explains the purpose of its anonymous function.

anonymous function: A function that has no function name.

The definition of function, first of all, a simple introduction to the definition of function, roughly divided into three ways

The first: This is also the most conventional one

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

The second: This method uses the function constructor, the argument list and function body as a string, very inconvenient, not recommended.

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

The third type:

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

Notice that the function to the right of "=" is an anonymous function that creates the function and assigns it to the variable square.

There is no block-level scope concept in JavaScript. That is, a variable that is defined in a block-level statement is actually created in the containing function (the external function) rather than in the statement.

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

The function in Java, C # and other languages, the variable I will only be defined in the FOR Loop statement, Loop end, I was destroyed. But in JavaScript, the variable i is defined in the Outputnumber () active object, so it can be accessed within the function as soon as it is defined. Even if you re declaring the same variable, it doesn't change its value.

function Outputnumber (count) {for 
  (var i=0;i<1000;i++) { 
    alert (i); 
  } 
  var i;   Re-declare Variable 
  alert (i);  Count 

Anonymous functions can be used to mimic block-level scopes and to avoid this problem, and the syntax for anonymous functions used as block-level scopes (also known as private scopes) is as follows:

(function () { 
   //This is a block-level scope 

The code definition above uses an anonymous function that contains a function declaration inside a parenthesis to indicate that it is a function expression. This function is called immediately after the other pair of parentheses.
Whenever you need some variables temporarily, you can use a private scope, such as:

function Outputnumber (count) { 
  (function () {for 
  (var i=0;i<1000;i++) { 
    alert (i); 
  }) (); 
  alert (i);  Cause an error 

In this way, we insert a private scope outside of the For loop. Any variables defined in an anonymous function are destroyed at the end of execution.

This technique is often used outside of a function in the global scope, limiting the addition of too many variables and functions to the global scope.

In general, we should minimize the addition of variables and functions to the global scope.

This approach reduces the need for closures to consume memory because there is no reference to anonymous functions, and the scope chain can be destroyed immediately as soon as the function has finished executing.

Related Article

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.