[FW]: javascript anonymous Function

Source: Internet
Author: User
ArticleDirectory
    • 1. What is an anonymous function?
    • Ii. Differences between function literal and function () constructor
    • Iii. Code mode of anonymous Functions
    • Iv. Application of anonymous Functions
Original article: javascript anonymous functions 1. What is an anonymous function?

There are three methods to define a function in javascript:

    1. Function keyword (function) Statement:
      Function fnmethodname (x) {alert (x );}
    2. Function 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 1st method is the most commonly used method. The last two methods copy a function to the variable fnmethodname, which has no name, that is, anonymous functions. In fact, many languages have anonymous functions.

Ii. Differences between function literal and function () constructor
    1. Although a function is an anonymous function, the syntax allows you to specify any function name for the function. When writing a recursive function, you can call it yourself. If you use function () to construct a function, you cannot.
      VaR F = function fact (x) {If (x <= 1) return 1; else return x * fact (x-1 );};
    2. Function () constructor allows JavaScript during runtimeCodeDynamic Creation and compilation. In this way, it is similar to the global function eval ().
    3. Function () constructor parses the function body each time it is executed and creates a new function object. Therefore, it is very inefficient to call function () constructor in a loop or frequently executed function. On the contrary, function literal is not re-compiled every time.
    4. When using function () to construct a function, it does not follow the typical scope. It always treats it as a top-level function for execution.
      Var y = "Global"; function constructfunction () {var y = "local"; return new function ("Return y ");// Unable to obtain local variables} Alert (constructfunction ()());// Output "Global"

Compared with function keyword definition, the function () constructor has many features and is difficult to use. Therefore, this technology is rarely used. The function literal expression is very similar to the function keyword definition. Considering the difference above, although there are bugs in some WebKit engines under OS X 10.4.3 for anonymous functions with literally sent messages, however, the anonymous functions we usually call all refer to anonymous functions in the form of functions literally. For more details, see the functions chapter in javascript: the definitive guide, 5th edition.

Iii. Code mode of anonymous Functions

Yesterday, hedger Wang introduced several code modes for anonymous functions in his blog:

Error Mode: It cannot work. The browser reports a syntax error.

 
Function () {alert (1 );}();
    1. Function literal: First declare a function object and then execute it.
      (Function () {alert (1 );})();
    2. Priority expression: Since the JavaScript Execution expression is from the inside of the parentheses to the outside, you can use parentheses to force the declaration function.
      (Function () {alert (2 );}());
    3. Void Operator: Use the void operator to execute a separate operand without parentheses.
      Void function () {alert (3 );}()

The three methods are equivalent. hedger Wang prefers 3rd for personal reasons, and I see 1st in actual applications.

Iv. Application of anonymous Functions
    1. The first sentence in Javascript's module mode is "global variables are the Devil ". With the VaR keyword, the anonymous function can effectively ensure that JavaScript is written on the page without causing global variable pollution. This is very effective and elegant when adding JavaScript to a page that is not very familiar. In fact, Yui and its corresponding examples use a lot of anonymous functions, and many other JavaScript libraries also use them.
    2. The cornerstone of functional programming in JavaScript. For details, see compiling beautiful JavaScript with functional programming technology and functional JavaScript programming guide.
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.