如何向某人解释JavaScript关闭,知道它们所构成的概念(例如功能、变量等),但不了解关闭本身?
我已经在维基百科上看到了图表的例子,但不幸的是,它没有帮助。
如何向某人解释JavaScript关闭,知道它们所构成的概念(例如功能、变量等),但不了解关闭本身?
我已经在维基百科上看到了图表的例子,但不幸的是,它没有帮助。
当前回答
因为所有这些外部变量,由一个经文所指的函数,实际上是其经文所关闭的函数链中的本地变量(全球变量可以假设是某种根函数的本地变量),并且每个函数的单一执行都会产生其本地变量的新例子,因此,每个函数的执行都会返回(或以其他方式转换)。
此外,必须明白,JavaScript中的本地变量不是在滑板框中创建的,而是在滑板上,只有当没有人提到它们时才会被摧毁。当一个函数返回时,其本地变量的参考会减少,但如果在当前执行期间,它们成为关闭的一部分,并且仍然被其经文所定义的函数所提到。
一个例子:
function foo (initValue) {
//This variable is not destroyed when the foo function exits.
//It is 'captured' by the two nested functions returned below.
var value = initValue;
//Note that the two returned functions are created right now.
//If the foo function is called again, it will return
//new functions referencing a different 'value' variable.
return {
getValue: function () { return value; },
setValue: function (newValue) { value = newValue; }
}
}
function bar () {
//foo sets its local variable 'value' to 5 and returns an object with
//two functions still referencing that local variable
var obj = foo(5);
//Extracting functions just to show that no 'this' is involved here
var getValue = obj.getValue;
var setValue = obj.setValue;
alert(getValue()); //Displays 5
setValue(10);
alert(getValue()); //Displays 10
//At this point getValue and setValue functions are destroyed
//(in reality they are destroyed at the next iteration of the garbage collector).
//The local variable 'value' in the foo is no longer referenced by
//anything and is destroyed too.
}
bar();
其他回答
查找描述的解释:如何在场景背后的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() 被召唤时,我们有以下范围链:
此分類上一篇
了解范围对象是如何创建,使用和删除,这是一个关键,有一个大图像,并了解如何在盖子下工作关闭。
请参见上述文章,详细信息。
也许你应该考虑一个以对象为导向的结构而不是内部功能。
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
此答案的版本图像: [ 解决]
只是忘记每件事的范围,并记住:当一个变量需要在某个地方,JavaScript不会破坏它。
例子1:
此分類上一篇
例子2:
此分類上一篇
例子3:
皮诺基奥:1883年关闭(JavaScript前一世纪)
我想这最好可以解释给一个有趣的冒险的6岁男孩......Pinocchio的冒险的一部分,在那里Pinocchio被一个过度的狗鱼吞下。
var tellStoryOfPinocchio = 函数(原始) { // 准备令人兴奋的事情发生 var pinocchioFindsMisterGeppetto; var happyEnding; // 故事开始在那里Pinocchio寻找他的“父亲” var pinocchio = { 名称:‘Pinocchio’,位置:‘在海里’,鼻子长度: 2 }; // 它是一只狗......它是一只鱼...... // 狗鱼出现,但是我
關閉的觀點:
封面可以与一本书,一本书标记,一本书架上进行比较。
这类似于关闭,书是外部函数,页面是你的内部函数,从外部函数返回,书标是你的页面的参考,故事的背景是你需要保存的语法范围。
代码例子:
function book() {
var pages = [....]; //array of pages in your book
var bookMarkedPage = 20; //bookmarked page number
function getPage(){
return pages[bookMarkedPage];
}
return getPage;
}
var myBook = book(),
myPage = myBook.getPage();
要考虑的几点:
点1:书架,就像函数板有有限的空间一样,所以要明智地使用它。
点2:想想事实,如果你只想跟踪一个页面时,你是否需要坚持整个书,你可以释放部分记忆,而不是在关闭回来时存储所有页面。