Javascript anonymous Functions

Source: Internet
Author: User
An anonymous function is a function that is dynamically declared at runtime. They are called anonymous functions because they do not have function names. An anonymous function is a function that is dynamically declared at runtime. They are called anonymous functions because they do not have function names.

Anonymous functions are defined through function expressions rather than function declaration syntax. You can use a function expression to create a new function wherever expressions can be placed. For example, you can define a new function as a parameter called by a function or as an attribute of another object.

The following is a typical naming function:

function flyToTheMoon(){    alert("Zoom! Zoom! Zoom!");}flyToTheMoon();

The following is the same example but is created as an anonymous function:

var flyToTheMoon = function(){    alert("Zoom! Zoom! Zoom!");}flyToTheMoon();

An anonymous function is created by a function expression.

In javascript, the two most commonly used function creation methods use the function declaration syntax and function expressions. An anonymous function is created using a function expression.

In a statement, if the function keyword first appears and follows a function name, this function is created by the function declaration Syntax:

When a function expression is called, it creates a new function object and returns it. Here is an example of creating a function and assigning it to a variable named flyToTheMoon:

var flyToTheMoon = function() {    alert("Zoom! Zoom! Zoom!");}

The value assignment here is almost the same as assigning the return value of any function to the variable. The only special difference is that this value is a function object rather than some simple numbers or dates.

This is possible because a function is only a special object in javascript. This means they can be used like other objects. They can be stored in variables, transmitted as parameters to other functions, or returned by return statements in functions. Functions are always objects, no matter how they are created.

Once a function is stored in a variable, this variable can be used to call this function:

flyToTheMoon();

An anonymous function is created at runtime.

Function expressions can be used in any place where expressions can be placed. For example, you can use a function expression when a variable is assigned a value. You can use a function expression when a parameter is passed to a function or in a return statement. This is possible because the function is always called at runtime.

The function declaration syntax is different. They run before any other code is executed, because the function does not need to be declared before it is called by the Code.

The function declaration syntax cannot be used to create anonymous functions because they require a function name. The function declaration syntax uses the function name to add it as a variable to the current scope.

No function name is required for anonymous functions.

This seems a bit strange, because how do you call a function without a name? This works because the function name is a little different from the variable that saves the function object.

A function created by the function declaration syntax always has a function variable with the same name as a function variable, because the function declaration syntax automatically creates this variable:

function flyToTheMoon() {    alert("Zoom! Zoom! Zoom!");}flyToTheMoon();

This name is optional for functions created by function expressions. In many cases, this name is not important to us, so we create an anonymous function without a name, just like this:

var flyToTheMoon = function() {    alert("Zoom! Zoom! Zoom");}flyToTheMoon();

However, if you want to, function expressions also support setting function names. The following are the same functions, but this time we have a function name:

var flyToTheMoon = function flyToTheMoon() {    alert("Zoom! Zoom! Zoom");}flyToTheMoon();

A name for your function does not automatically add a variable named after the function in the scope. You still need to assign the return value of the function expression to a variable.

In the previous example, the variable that saves the function object is the same as the function name, but it does not need to be like this:

var thingsToDoToday = function flyToTheMoon() {    alert("Zoom! Zoom! Zoom");}thingsToDoToday();

Why do we need a name?

The function name can be used to call itself from within the function. This is useful in recursive functions.

var thingsToDoToday = function flyToTheMoon() {    if(!onTheMoon)        flyToTheMoon();    else        alert("One small step for a man..");}thingsToDoToday();

This is also useful for debugging, because you can see the function name in the call stack. The anonymous functions in the call stack usually look the same. If you are faced with a very disgusting debugging situation, sometimes giving a name to the function you are interested in can make the problem clearer.

Why does an anonymous function work?

You do not need to set a name for an anonymous function for convenience. After all, the name of the function is not important in many cases. In most cases, anonymous functions and named functions can perform most tasks well.

Functions created by function expressions are sometimes very useful.

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.