The use of anonymous functions in JavaScript and their advantages and disadvantages _ Basics

Source: Internet
Author: User
Tags anonymous

Anonymous functions are effective in ensuring that JavaScript is written on the page without causing global variables to be contaminated.

This is very effective and graceful when adding JavaScript to a page that is not very familiar.

One, what is an anonymous function?

There are generally three ways to define a function in javascript:

Functions keyword (function) statement:

function Fnmethodname (x) {alert (x);}

Functions literal (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 1th is the most commonly used method, the latter two are to copy a function to the variable fnmethodname, and this function is not a name, that is, anonymous functions.

In fact, quite a few languages have anonymous functions.

The difference between function literals and functions () constructors

Although the function literal is an anonymous function, the syntax allows you to specify any function name, and you can call itself when you write a recursive function, which is not possible with the function () constructor.

var f = function fact (x) {if (x < = 1) return 1; else return x*fact (x-1);

The function () constructor allows run-time JavaScript code to be dynamically created and compiled. In this way it resembles the global function eval ().

The function () constructor resolves the body of the functions every time it executes and creates a new function object. Therefore, it is very inefficient to call function () constructors in a loop or in frequently executed functions. Instead, the literal volume of a function is not recompiled every time it is encountered.

When a function () constructor is created, it does not follow a typical scope, and it is performed as a top-level function.

var y = "global";

function constructfunction () {var y = ' local '; return new Function (' Return y ');//Cannot get locals} alert (Constructfunction ()) ); The output "global" and the function keyword definitions have their own characteristics and are more difficult to use than the functions () constructor.

So this technique is usually rarely 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.

Code patterns for anonymous functions

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

function () {alert (1);} ();

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

(function () {alert (1);}) ( );

Precedence expression:

(function () {alert (2);} ( ) );

void operator:

void function () {alert (3);} () These three ways are equivalent, Hedger Wang for personal reasons prefer the 3rd kind, and in the actual application I see and use is the 1th kind.

Iv. Application of anonymous function

The first sentence of a modular pattern of JavaScript is "global variable is the Devil".

With the VAR keyword, anonymous functions can effectively ensure that JavaScript is written on the page without causing global variables to be contaminated.

This is very effective and graceful when adding JavaScript to a page that is not very familiar.

In fact, Yui and its corresponding examples of a large number of use of anonymous functions, other JavaScript libraries are also a large number of uses.

The cornerstone of JavaScript's functional programming (functional programming).

See "Using functional programming techniques to write graceful JavaScript" and "Functional JavaScript Programming Guide."

The above JavaScript in the use of anonymous functions and advantages and disadvantages of the detailed is small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.