如何向某人解释JavaScript关闭,知道它们所构成的概念(例如功能、变量等),但不了解关闭本身?
我已经在维基百科上看到了图表的例子,但不幸的是,它没有帮助。
如何向某人解释JavaScript关闭,知道它们所构成的概念(例如功能、变量等),但不了解关闭本身?
我已经在维基百科上看到了图表的例子,但不幸的是,它没有帮助。
当前回答
最好的方法是不断地解释这些概念:
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中的“关闭”。
其他回答
函数和函数的外部范围(语法环境)的参考
函数 汽车(制造商,模型,年,颜色) { 返回 { 到String() { 返回 `${ 制造商} ${ 模型} (${ 年}, ${ 颜色})` } } 组车 = 新车(‘Aston Martin’,‘V8 Vantage’,‘2012’,‘Quantum Silver’) console.log(car.toString())
事件导向的编程
在下面的例子中,所有实施细节都隐藏在即时执行的函数表达式内。 函数标记和 toString 接近私人状态和函数他们需要完成工作。 关闭已使我们能够模块化和包容我们的代码。
例子1
这个例子表明,本地变量在关闭中没有复制:关闭保持了对原始变量的参考。
左边
关闭是通过内部函数可以提到在其外部关闭函数中存在的变量,其母函数已经结束后。
// A function that generates a new function for adding numbers.
function addGenerator( num ) {
// Return a simple function for adding two numbers
// with the first number borrowed from the generator
return function( toAdd ) {
return num + toAdd
};
}
// addFive now contains a function that takes one argument,
// adds five to it, and returns the resulting number.
var addFive = addGenerator( 5 );
// We can see here that the result of the addFive function is 9,
// when passed an argument of 4.
alert( addFive( 4 ) == 9 );
function sing(person) {
var firstPart = "There was " + person + " who swallowed ";
var fly = function() {
var creature = "a fly";
var result = "Perhaps she'll die";
alert(firstPart + creature + "\n" + result);
};
var spider = function() {
var creature = "a spider";
var result = "that wiggled and jiggled and tickled inside her";
alert(firstPart + creature + "\n" + result);
};
var bird = function() {
var creature = "a bird";
var result = "How absurd!";
alert(firstPart + creature + "\n" + result);
};
var cat = function() {
var creature = "a cat";
var result = "Imagine That!";
alert(firstPart + creature + "\n" + result);
};
fly();
spider();
bird();
cat();
}
var person="an old lady";
sing(person);
指示
我没有时间去购物,所以函数需要知道我们在冰箱里有什么来做出决定。 每个成分都有不同的烹饪时间,我们希望所有的东西都被机器人同时加热。 我们需要提供函数与我们喜欢的数据,函数可以“说话”到冰箱,函数可以控制机器人。
function cookMeal() { /* STUFF INSIDE THE FUNCTION */ }
function sing(person) { /* STUFF INSIDE THE FUNCTION */ }
因此,这个功能可能与歌曲有关系,可能需要一些关于一个人的数据。
现在,在函数歌(),接近代码的结尾是线
var person="an old lady";
全球变量:人是一个全球变量,这意味着如果你将其值从“老太太”变成“年轻人”,这个人将继续是年轻人,直到你决定再次改变它,而代码中的任何其他功能可以看到它是一个年轻人。
sing(person);
这条线叫这个功能,就好像它叫一个狗。
关闭后,线路来了。
fly();
spider();
bird();
cat();
关闭是一种功能,即使关闭了父母功能后,也可以访问父母范围。
var add = (function() {
var counter = 0;
return function() {
return counter += 1;
}
})();
add();
add();
add();
// The counter is now 3
例子解释:
变量添加被指定为自我召唤函数的回报值. 自我召唤函数只运行一次. 它将计算器设置为零(0),并返回函数表达式. 这种方式添加变成函数. “奇妙”部分是它可以在母范围内访问计算器. 这被称为JavaScript关闭。
来源
最好的方法是不断地解释这些概念:
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中的“关闭”。