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

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


当前回答

这个答案是这个YouTube视频JavaScript关闭的总结。

关闭只是保持其私人变量的状态的国家功能。

此分類上一篇

但现在有情况下,我们希望保持这个功能状态,即JavaScript关闭会使用。 关闭是内部函数的功能,如下代码所示的返回通话。

此分類上一篇

function Counter() {
           var counter = 0;

           var Increment = function () {
               counter++;
               alert(counter);
           }
           return {
               Increment
           }
       }

var x = Counter(); // get the reference of the closure
x.Increment(); // Displays 1
x.Increment(); // Display 2 ( Maintains the private variables)

但现在最大的问题是如何使用这种可靠的功能。 可靠的功能是构建区块,以实施OP概念,如抽象、包容和创建自我包含的模块。

因此,任何你想要的包容,你可以把它作为私人和事物被曝光给公众应该放回声明。 此外,这些组件是自我包含的隔离物体,所以它们不会污染全球变量。

遵循 OOP 原则的对象是自我包含的,遵循抽象,遵循包容等。

此分類上一篇

其他回答

我需要知道一个按钮被点击了多少次,每次第三点击都会做点什么。

考虑此选项

var element = document.getElementById('按钮'); element.addEventListener(“点击”,(函数() { // init 计算到 0 var 计算 = 0; 返回函数(e) { // <- 这个函数变成点击处理器计算++; // 并将保持访问上述 ` 计算` 如果(计算 === 3) { // 做点什么每第三次 console.log(“第三次是魅力!”); // 恢复 c

简单的单线关闭

//          _______________________Immediately invoked______________________
//         |                                                                |
//         |        Scope retained for use      ___Returned as the____      |
//         |       only by returned function   |    value of func     |     |
//         |             |            |        |                      |     |
//         v             v            v        v                      v     v
var func = (function() { var a = 'val'; return function() { alert(a); }; })();

func();  // Alerts "val"
func.a;  // Undefined

因此,在我们的主要例子中,计算变量包含在关闭室内,并始终可用于活动交易者,因此它保持其状态从点击到点击。

在那里你去;现在你正在完全包容这种行为。

我只是将它们指向Mozilla关闭页面,这是我发现的关闭基础和实用使用的最佳,最简短和简单的解释,强烈建议任何人学习JavaScript。

而且是的,我甚至会推荐给一个6岁的人 - 如果6岁的人正在学习关闭,那么他们准备好理解文章中提供的简短和简单的解释。

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

    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

认真对待这个问题,我们应该知道什么一个典型的6岁的人能够认知,但承认的是,一个对JavaScript感兴趣的人并不那么典型。

关于童年发展: 5 至 7 年,它说:

我们可以在JavaScript中编码此类内容:

函数 makeKitchen() { var trashBags = ['A', 'B', 'C']; // 只有 3 在第一次返回 { getTrashBag: 函数() { return trashBags.pop(); }; } var 厨房 = makeKitchen(); console.log(kitchen.getTrashBag()); // 返回垃圾袋 C console.log(kitchen.getTrashBag()); // 返回垃圾袋 B console.log(kitchen.getTrashBag()); // 返回垃圾袋 A

每次做Kitchen() 被召唤,一个新的关闭创建与自己的单独的废物Bags. 废物Bags 变量是本地到每个厨房的内部,并不能在外面访问,但内部功能在 getTrashBag 属性有访问它。

考虑到以下功能

function person(name, age){

    var name = name;
    var age = age;

    function introduce(){
        alert("My name is "+name+", and I'm "+age);
    }

    return introduce;
}

var a = person("Jack",12);
var b = person("Matt",14);

每次函数人被称为一个新的关闭创建,而变量 a 和 b 具有相同的输入函数,它与不同的关闭有关,而这个关闭仍然存在,即使函数人完成执行后。

此分類上一篇

a(); //My name is Jack, and I'm 12
b(); //My name is Matt, and I'm 14

一个抽象的封闭可以代表到这样的东西:

closure a = {
    name: "Jack",
    age: 12,
    call: function introduce(){
        alert("My name is "+name+", and I'm "+age);
    }
}

closure b = {
    name: "Matt",
    age: 14,
    call: function introduce(){
        alert("My name is "+name+", and I'm "+age);
    }
}

假设你知道一个班级在另一个语言是如何工作的,我会做一个类似。

想想像

JavaScript 作为构建器的本地变量作为例子属性这些属性是私人内部功能作为例子方法

每一天都有一个功能被称为

将创建一个包含所有本地变量的新对象,该对象的方法将访问该对象的“属性”。