The usage of anonymous function in JS and its advantages and disadvantages __ function

Source: Internet
Author: User
The usage and advantages and disadvantages of anonymous function in JS

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 the 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 a 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 created and compiled dynamically. In this way it resembles the global function eval (). The
function () constructor resolves the body of the function 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. The
Use function () constructor does not follow a typical scope when it is created as a top-level function.
var y = "global";
Function Constructfunction () {var y = ' local '; return new function (' Return y ');//Cannot get locals} alert (Constructfunctio N () ()); The output "global" and the function keyword definitions have their own characteristics and are more difficult to use than the functions () constructors,
So this technique is often rarely used. The
and function literal expressions are 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 our usual anonymous function refers to an anonymous function that takes the form of a function literal. Three, 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.

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.