Anonymous functions for Avascript

Source: Internet
Author: User

First, what is an anonymous function?

There are three ways of defining a function in javascript:

    1. function-keyword statement :
      function fnMethodName(x){alert(x);}
    2. function literal (functions literals):
      var fnMethodName = function(x){alert(x);}
    3. function () constructor :
      var fnMethodName = new Function(‘x‘,‘alert(x);‘)

The above three methods define the same method function Fnmethodname, the 1th is the most commonly used method, the latter two are to copy a function to the variable fnmethodname, and this function is no name, that is, anonymous function.

The difference between the function literal and the functions () constructor
    1. Although the function literal is an anonymous function, the syntax allows you to specify any function name for it, and you can call it yourself when writing a recursive function, but not with the function () constructor.
      var f = function fact(x) {  if (x < = 1) return 1;  else return x*fact(x-1);};
    2. The function () constructor allows runtime JavaScript code to be created and compiled dynamically. In this way it resembles the global function eval ().
    3. The function () constructor parses the body of the functions each time it executes and creates a new function object. Therefore, it is very inefficient to invoke the function () constructor in a loop or in a frequently executed functions. Instead, the function literal is not recompiled every time it is encountered.
    4. Creating a function using the function () constructor does not follow a typical scope, it always treats it as a top-level function to execute.
      var y = "global";function constructFunction() {    var y = "local";    return new Function("return y");  //  无法获取局部变量}alert(constructFunction()());  // 输出 "global"

The function () constructor has its own characteristics and is much more difficult to use than the Functions keyword definition, so this technique is often seldom used. The function literal expression is very close to the definition of the function keyword. Consider the previous distinction, although there is a message that the literal anonymous function has bugs under some WebKit engines under OS X 10.4.3, but what we normally call anonymous functions refers to anonymous functions that take the form of function literals. More details can be found in the functions chapter of the Javascript:the Definitive Guide, 5th Edition.

Third, the code pattern of anonymous function

error mode : It does not work, the browser will report syntax error.

function(){  alert(1);}();
    1. function literal : Declare a Function object first, and then execute it.
      (function(){  alert(1);} ) ( );
    2. Precedence expression : Because the JavaScript execution expression is from inside the parentheses to the outside, you can enforce the declared function with parentheses.
      ( function(){  alert(2);} ( ) );
    3. void operator : Use the void operator to execute a single operand that is not enclosed in parentheses.
      void function(){  alert(3);}()

These three ways are equivalent.

Anonymous functions for Avascript

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.