JavaScript anonymous functions and closures

Source: Internet
Author: User
Tags closure value of pi

Anonymous functions are relative to the name of a function

Functions such as function Setage () {} are named Setage

and the function () {} is an anonymous

A function with a name can be written as setage (), while a function without a name is executed to be written (function ()) ();

Another way is to assign an anonymous function to a variable to use

var a=function() {alert ("Hello,world");} A ();

anonymous function pass parameter

(function(age) {return age ;}) (100);

anonymous function Generation closure

A closure is simply a function in which there are functions (generally anonymous functions), see the code:

function box () {var a=10; return function () {alert ("Hello")}}
Box ()//Return function () {alert ("Hello")} A string of code
Box () ()//plus one () to execute the anonymous function, return hello

On the closure of the explanation, the above description is too vulgar, the following specific instructions

A function that has access to a variable within the scope of another function is a closed packet

The closure originates from the JS scope chain problem, the local variable can access the global variable directly in JS, and the global variable cannot access the local variable

function box () {
var a=10;

}

Alert (a)//undefined, global cannot access the var a=10 variable in the function body, but sometimes we need external to get this local a variable, then what do we do? It's a closed-bag approach.

Using the local variables in JS can directly access the global variables, and the global variable cannot access the principle of local variables, we build a function in box () {}, which var a for this newly built function is his "global variables"

For example

function getcircle () {var pi=3.1415926;//local variable, how to get the outside alert to obtain pi? returnfunction () {//write a function, this function can get the value of Pi  return  pi;//This pi value once called getcircle () can return to the anonymous function of this pi}}alert ( Getcircle () ());

What are the benefits of using closures?

In JS once the function is executed, the variables inside will be garbage collected, destroyed, it can be understood that the local variables in JS takes precedence over the global variable execution, after the function body executes, it is destroyed, so the global variable can not access the local. In some cases, we need to save the local variables, and we're going to use closures.

Summary: Closure Benefits:

One is the variable that can read the inside of the function as mentioned earlier, and the other is to keep the values of these variables always in memory.

function box () {var a=0; return function () {    a=a+1; return A;}} var b=Box (); B (); // 1B (); // 2

B () var A is not zeroed every time it is executed, but the last result is preserved!

JavaScript anonymous functions and closures

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.