如何向某人解释JavaScript关闭,知道它们所构成的概念(例如功能、变量等),但不了解关闭本身?

我已经在维基百科上看到了图表的例子,但不幸的是,它没有帮助。


当前回答

此答案的版本图像: [ 解决]

只是忘记每件事的范围,并记住:当一个变量需要在某个地方,JavaScript不会破坏它。

例子1:

此分類上一篇

例子2:

此分類上一篇

例子3:

其他回答

也许你应该考虑一个以对象为导向的结构而不是内部功能。

    var calculate = {
        number: 0,
        init: function (num) {
            this.number = num;
        },
        add: function (val) {
            this.number += val;
        },
        rem: function (val) {
            this.number -= val;
        }
    };

并从 calculate.number 变量中阅读结果,谁需要“返回”无论如何。

//Addition
First think about scope which defines what variable you have to access to (In Javascript);

//there are two kinds of scope
Global Scope which include variable declared outside function or curly brace

let globalVariable = "foo";

一件事要记住,一旦你宣布了一个全球变量,你可以在你的代码中的任何地方使用它,即使在功能中;

包含仅在您的代码的特定部分可用的变量的本地范围:

函数范围是当您在函数中宣布变量时,您只能在函数内访问变量。

function User(){
    let name = "foo";
    alert(name);
}
alert(name);//error

//Block scope is when you declare a variable within a block then you can  access that variable only within a block 
{
    let user = "foo";
    alert(user);
}
alert(user);
//Uncaught ReferenceError: user is not defined at.....

//A Closure

function User(fname){
    return function(lname){
        return fname + " " lname;
    }
}
let names = User("foo");
alert(names("bar"));

//When you create a function within a function you've created a closure, in our example above since the outer function is returned the inner function got access to outer function's scope

我喜欢Kyle Simpson的关闭定义:

关闭是函数能够记住和访问其语法范围,即使该函数在其语法范围之外运行。

语法范围是当一个内部范围能够访问其外部范围时。

此分類上一篇: You Don't Know JS: Scopes & Closures

function foo() {
  var a = 2;

  function bar() {
    console.log( a );
  }
  return bar;
}

function test() {
  var bz = foo();
  bz();
}

// prints 2. Here function bar referred by var bz is outside 
// its lexical scope but it can still access it
test(); 

最好的方法是不断地解释这些概念:

console.log(x);
// undefined

var x = 42;
console.log(x);
// 42

现在,JavaScript知道X是什么意思。

x = 43;
console.log(x);
// 43

现在,X意味着别的东西。

范围

当您创建一个函数时,该函数为变量具有自己的“盒子”。

function A() {
  var x = 42;
}

console.log(x);

// undefined

var x = 42;

function A() {
  console.log(x);
}

// 42

在函数 A 中,您有“范围访问”到 x。

function A() {
  var x = 42;
}

function B() {
  console.log(x);
}

// undefined

function A() {

  var x = 42;

  function B() {
    console.log(x);
  }

}

// 42

功能

在JavaScript中,您通过称之为:

function A() {
  console.log(42);
}

A();

// 42

var a = function() {
  console.log(42);
};

变量现在意味着一个功能,你可以运行它。

a();
// 42

setTimeout(a, 1000);

在一秒钟(1000毫秒),函数一个点被称为:

// 42

现在,当你定义函数时,这些函数可以访问它们的外部目标。

var a = function() {

  var text = 'Hello!'

  var b = function() {
    console.log(text);
    // inside function `b`, you have access to `text`
  };

  // but you want to run `b` later, rather than right away
  setTimeout(b, 1000);

}

现在发生了什么?

// 'Hello!'

var c;

var a = function() {

  var text = 'Hello!'

  var b = function() {
    console.log(text);
    // inside function `b`, you have access to `text`
  };

  c = b;

}

// now we are out side of function `a`
// call `a` so the code inside `a` runs
a(); 

// now `c` has a value that is a function
// because what happened when `a` ran

// when you run `c`
c();

// 'Hello!'

您仍然可以在关闭范围内访问变量。

即使一个已经完成了运行,现在你正在运行C的外部。

在这里发生的事情被称为JavaScript中的“关闭”。

查找描述的解释:如何在场景背后的JavaScript关闭工作。

文章解释了范围对象(或LexicalEnvironments)如何分配并以直观的方式使用。

"use strict";

var foo = 1;
var bar = 2;

function myFunc() {
  //-- Define local-to-function variables
  var a = 1;
  var b = 2;
  var foo = 3;
}

//-- And then, call it:
myFunc();

在执行顶级代码时,我们有以下范围对象的安排:

此分類上一篇

当 myFunc() 被召唤时,我们有以下范围链:

此分類上一篇

了解范围对象是如何创建,使用和删除,这是一个关键,有一个大图像,并了解如何在盖子下工作关闭。

请参见上述文章,详细信息。

下面的例子是JavaScript关闭的一个简单的描述,这是关闭函数,返回函数,并访问其本地变量x,

function outer(x){
     return function inner(y){
         return x+y;
     }
}

接下来的函数如下:

var add10 = outer(10);
add10(20); // The result will be 30
add10(40); // The result will be 50

var add20 = outer(20);
add20(20); // The result will be 40
add20(40); // The result will be 60