Home  >  Article  >  Web Front-end  >  Explain what anonymous functions are in Javascript

Explain what anonymous functions are in Javascript

青灯夜游
青灯夜游forward
2018-10-12 15:56:465262browse

This article will introduce to you what anonymous functions are in Javascript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Analysis:

1. The so-called anonymous function, understood literally, is a function without a name. JS uses () instead (Note, it is the parentheses in the English state

2. Definition form:

function (){
    //to add codes that you want to add
}

3. Function of anonymous function

(1) Compared with closure function, its biggest function is not to pollute the global object. Once execution is completed, GC automatically reclaims memory, which is the essential difference from closure functions. A major feature of the closure function is that the

variable is resident in memory and is only released 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 was run twice, the first time the value was 999, the second time the value was 1000. This proves that the local variable n in function f1 is always stored in memory and is not automatically cleared after f1 is called.

Why is this so? 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 the existence of f2 depends on f1, so f1 is always in memory and will not be deleted after the call is completed. , recycled by the garbage collection mechanism (garbage collection).

Another thing worth noting in this code is the line "nAdd=function(){n =1}". First of all, the var keyword is not used before nAdd, so nAdd is a global variable. rather than local variables. Secondly, the value of nAdd is an anonymous function, and this

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

(2) Compared with general functions, precompilation is not performed.

function fuc() {
    fuc1(); //foo被提到了作用域的最前面, 于是这里可以正常调用foo函数
    fuc2(); //这里会报错bar是个undefined
    function fuc1() {alert("foo()") }
    var fuc2 = function () { alert("bar")};
}

Code:

Several expressions of anonymous functions:

Mode 1: Function Literal(Function Literal)

First declare the function object, Then execute.

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

Mode 2: Priority Expression (Prior Expression)

Because JavaScript executes expressions in order from the inside to the outside, parentheses are used to force the execution of declared functions.

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

Mode 3: Void Operator (Void Operator)

Use the Void operator to execute a single operand.

void function(){ 
// insert code here 
}();

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study. For more related tutorials, please visit JavaScript Video Tutorial!

Related recommendations:

JavaScript Graphic Tutorial

JavaScript Online Manual

The above is the detailed content of Explain what anonymous functions are in Javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete