The seven-medium form of JS closure

Source: Internet
Author: User

It's not easy to get a closer look at closures;

One of the most common forms is that a function is returned as a return value

var function () {    var b = ' local ';     var function () {        return  b;    }     return N;} Console.log (F () ());

function Assignment Value

One form of deformation is to assign an intrinsic function to an external variable

var Inner; var function () {    var b = ' local ';     var function () {        return  b;    };     = N;}; F (); Console.log (inner ());
function parameters

Closures can be implemented in the form of function parameter transfer functions

var function (FN) {    Console.log (FN ());} var function () {    var b = ' local ';     var function () {        return  b;    }    Inner (N);} F ();

In fact, the above three kinds of writing, a kind of the feeling of the bottle;

function Inner (FN) {    Console.log (FN ());} (function() {    var b = ' local ';     var function () {        return  b;    }    Inner (N);}) ();
G (s) etter

We provide getter () and setter () functions to store the variables that will be manipulated inside the function to prevent them from exposing the outer part

varGetvalue,setvalue; (function(){    varSecret = 0; GetValue=function(){        returnsecret; } setValue=function(v) {if(typeofv = = = ' Number ') {Secret=v; }}) (); Console.log (GetValue () );//0SetValue (1); Console.log (GetValue ());//1
Iterators

We often use closures to implement an accumulator

var add = (function() {    var counter = 0;     return function () {        return + +counter;     }}) (); Console.log (Add ())//1console.log (Add ())//2  

On the definition of closures: this is more than a drop AH;

Ancient definition

Closure (closure), refers to the function variable can be stored in the function scope, so it appears that the function of the variable "wrapped" up

So, the function that contains the variable is the closure

// According to the old definition, the function foo that contains the variable n is the closure function foo () {    var n = 0;} Console.log (n)//uncaught referenceerror:n is not defined

A closure is a function that can access the scope of which it is located

So, the function that needs to find the variable through the scope chain is the closure

// according to definition two, the bar function nested in the Foo function is the closure function foo () {    var a = 2;     function Bar () {        //  2    }    Bar ();} Foo ();

Define three (the most common drop)

Closures are functions that are called outside the scope of the function declaration

Calling a function outside the scope of the function declaration requires that the function be passed as a return value or as a parameter

function foo () {    var a = 2;     return function () {        console.log (a); // 2     }}foo () ();

Iife, is it a closed bag?

The Foo () function is defined in the global scope and is immediately called in the global scope, if it is a closure by definition. If you follow definition two and define three, it's not a closure.

var a = 2;(function  foo () {    console.log (a); // 2}) ();

Strictly speaking, closures need to meet three conditions: "1" Access scope; "2" function nesting; "3" is called outside the scope of the action

Some people think that only meet the conditions of 1, so Iife is a closure, some people feel satisfied with the conditions 1 and 2 can be, so the nested function is the closure; some people think that 3 conditions are satisfied, so the function that is called outside the scope is the closure

The question is, who is the authority?

In fact, the nature of closures is the scope of the problem, the variables of the survival of the scope, variables or references before the loop nesting, resulting in the dependency between scopes ... This is very abstract, specifically, we have to open examples, such as my side article: http://www.cnblogs.com/mc67/p/4801422.html

With the conditions for implementing closures and whether it is a closure, we have to separate the area;

The seven-medium form of JS closure

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.