JavaScript anonymous function closure

Source: Internet
Author: User
Tags closure

anonymous functions

(function () {
Console.info ("111111111");
})();

var my = (function () {
return "11111111";
})();

function to put anonymous function called closure

function my () {
return function () {
Return "Zhang San";
}
}

Console.info (My () ());

Advantages of closures, avoiding the use of global variables, providing access to local variables

Save the local variables.

function box () {
var age = 100;

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

}

var b = box ();
b ();
b ();
b ();

function Box1 () {

var arr = [];

for (Var i=0;i<5;i++) {
Arr[i] = (function (num) {
return num;
}) (i)

}
return arr;

}

var my = function () {
return "11111111";
}();

Closures are in the global direction of window,

var name = "Global";
var user ={
Name: "Zhang San",
Getuser:function () {
var = this;
return function () {
Return that.name;//returns the properties of the user object, not the global
}
}
}

Block-level scope, (function () {

})();

JavaScript anonymous function closure

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.