Nineth. The anonymous function of JavaScript

Source: Internet
Author: User
Tags closure

Analysis:

1, the so-called anonymous function, from the literal meaning of understanding, is no name of the function, JS with () to replace ( note, is the English state of parentheses )

2, the definition of the form:

function () {    //to add codes that's want to add}

3. Function of anonymous functions

(1) Compared with the closure function, the maximum effect is not to pollute the global object, once executed, the GC automatically reclaims the memory, which is the nature of the closure function and the difference. A major feature of the closure function is:

Variable resident memory, released only when the browser is closed.

Function F1 () {var n=999;    Nadd=function () {n+=1} function F2 () {alert (n);  } return F2;  } var result=f1 (); Result ();  999 Nadd (); Result (); 1000

In the above code, result is actually the closure F2 function. It runs altogether two times, the first value is 999, the second value is 1000. This proves that the local variable n in the function F1 is kept in memory and is not automatically cleared after the F1 call.

Why is that? The reason is that F1 is the parent function of F2, and F2 is assigned to a global variable, which causes F2 to always be in memory, and F2 's presence depends on F1, so F1 is always in memory and will not be reclaimed by the garbage collection mechanism (garbage collection) after the call ends.

Another notable part of this code is the line "nadd=function () {n+=1}", which first did not use the var keyword in front of Nadd, so Nadd is a global variable, not a local variable. Second, the value of Nadd is an anonymous function (anonymous functions), and this

The anonymous function itself is also a closure, so nadd is equivalent to a setter that can manipulate local variables inside the function outside of the function.

(2) The pre-compilation is not performed compared to the general function.

function fuc () {    fuc1 ();//foo is referred to the front of the scope, so you can call the Foo function    fuc2 () properly here;//error here Bar is a undefined    function fuc1 () {Alert ("foo ()")}    var fuc2 = function () {alert ("Bar")};}

Code:

Several forms of representation of anonymous functions:

Pattern One: function literal (functions Literal)

Declare the function object first and then execute.

(function () {//Insert code here}) ();

Pattern Two: Precedence expressions (Prior expression)

Because JavaScript executes an expression from inside to outside, the parentheses are used to enforce the declared function.

Mode three: void operator (void Operator)

Use the Void operator to perform a single operand.

  


 

Nineth. The anonymous function of JavaScript

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.