Usage and advantages and disadvantages of anonymous functions in JavaScript

Source: Internet
Author: User

Usage and advantages and disadvantages of anonymous functions in JavaScript

Anonymous functions can effectively write Javascript 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.

1. What is an anonymous function?

There are three methods to define a function in Javascript:

Function keyword statement:

Function fnMethodName (x) {alert (x );}

Function Literals ):

Var fnMethodName = function (x) {alert (x );}

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 are to copy a function to the variable fnMethodName, which has no name, that is, an anonymous function.

In fact, many languages have anonymous functions.

Ii. Differences between Function literal and Function () constructor

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 );};

Function () constructor allows dynamic creation and compilation of Javascript code during runtime. In this way, it is similar to the global function eval ().

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.

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"); // The local variable cannot be obtained} alert (constructFunction ()()); // The output "global" is more difficult to use than the Function keyword definition,

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.

Iii. Code mode of anonymous Functions

Error Mode: it cannot work, and the browser reports a syntax error.

Function () {alert (1 );}();

Function literal: first declare a function object and then execute it.

(Function () {alert (1 );})();

Priority expression:

(Function () {alert (2 );}());

Void OPERATOR:

Void function () {alert (3) ;}() is equivalent to the three methods. hedger wang prefers 3rd for personal reasons, in actual application, I see and use 1st types.

Iv. Application of anonymous Functions

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.

The cornerstone of functional programming in Javascript.

For details, see compiling beautiful JavaScript with functional programming technology and functional JavaScript programming guide.

The usage, advantages, and disadvantages of anonymous functions in the above JavaScript text are all the content shared by the editor. I hope you can give us a reference and support for the help house.

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.