如何向某人解释JavaScript关闭,知道它们所构成的概念(例如功能、变量等),但不了解关闭本身?
我已经在维基百科上看到了图表的例子,但不幸的是,它没有帮助。
如何向某人解释JavaScript关闭,知道它们所构成的概念(例如功能、变量等),但不了解关闭本身?
我已经在维基百科上看到了图表的例子,但不幸的是,它没有帮助。
当前回答
关闭是通过内部函数可以提到在其外部关闭函数中存在的变量,其母函数已经结束后。
// 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 );
其他回答
JavaScript 的关闭与 scopes 的概念有关。
在 es6 之前,没有区块级范围,只有 JS 的功能级范围。
也就是说,每当需要区块级范围时,我们都需要将其插入一个函数中。
看看这个简单而有趣的例子,关闭如何解决这个问题在ES5
// 让我们说我们只能使用一个传统的路径,而不是forEach为(var i = 0; i < 10; i++) { setTimeout(函数() { console.log('没有关闭访问指数 - '+ i) }) ) } // 这将打印10次“访问指数 - 10”,这是不正确的 /** 预计输出是访问指数 - 0 访问指数 - 1.. 访问指数 - 9 **/ // 我们可以通过使用 cl 解决它
注:这可以轻松地通过使用 es6 取代 var 来解决,因为它创造了语法范围。
简而言之,在 JS 中关闭仅仅是访问功能范围。
下面的例子是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
关闭是一种内部函数,可以访问外部(关闭)函数的变量 - 范围链. 关闭有三个范围链:它可以访问自己的范围(在曲线手臂之间定义的变量),它可以访问外部函数的变量,并且可以访问全球变量。
function showName(firstName, lastName) {
var nameIntro = "Your name is ";
// this inner function has access to the outer function's variables, including the parameter
function makeFullName() {
return nameIntro + firstName + " " + lastName;
}
return makeFullName();
}
console.log(showName("Michael", "Jackson")); // Your name is Michael Jackson
此外,这是一个经典的关闭方式在jQuery,每个JavaScript和jQuery开发人员使用它很多:
$(function() {
var selections = [];
$(".niners").click(function() { // this closure has access to the selections variable
selections.push(this.prop("name")); // update the selections variable in the outer function's scope
});
});
实用关闭
//javascript function makeSizer(size) { return function() { document.body.style.fontSize = size + 'px'; } var size12 = makeSizer(12); var size14 = makeSizer(14); var size16 = makeSizer(16); document.getElementById('size-12').onclick = size12; document.getElementById('size-14').onclick = size14; document.getElementById('size-16').onclick = size16; /*css*/ body {字体
孩子们永远不会忘记他们与父母分享的秘密,即使父母离开后。
JavaScript 功能的秘密是私人变量
var parent = function() {
var name = "Mary"; // secret
}
正如你可以猜测的那样,因为变量每次函数被召唤,没有人会知道它们,必须有一个秘密的地方,它们被存储在那里,它可以被称为秘密室或管道或地方范围,但这并不重要,我们知道它们在某个地方隐藏在记忆中。
但是,在JavaScript中,这是一个非常特殊的事情,在其他功能内创建的功能,也可以知道父母的本地变量,并保持它们,只要他们活着。
var parent = function() {
var name = "Mary";
var child = function(childName) {
// I can also see that "name" is "Mary"
}
}
因此,只要我们在父母功能,它可以创建一个或多个儿童功能,从秘密位置分享秘密变量。
但可悲的是,如果孩子也是父母功能的私人变量,当父母结束时也会死亡,而秘密也会与他们一起死亡。
所以要活着,孩子必须离开,直到太晚了。
var parent = function() {
var name = "Mary";
var child = function(childName) {
return "My name is " + childName +", child of " + name;
}
return child; // child leaves the parent ->
}
var child = parent(); // < - and here it is outside
所以,如果你叫孩子“爱丽丝”,她会回答。
child("Alice") => "My name is Alice, child of Mary"
这就是所有要说的。
也许你应该考虑一个以对象为导向的结构而不是内部功能。
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