javascript - 哪个函数是真正的闭包?

标签 javascript scope closures

来自 Mozilla 文档 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures :

function makeFunc() {
  var name = "Mozilla";
  function displayName() {
    alert(name);
  }
  return displayName;
}

var myFunc = makeFunc();
myFunc();

displayName 不是实际的闭包吗,因为它关闭了其外部函数 makeFunc 的范围以供以后使用? Mozilla 说 makeFunc 是闭包:

The solution to this puzzle is that myFunc has become a closure. A closure is a special kind of object that combines two things: a function, and the environment in which that function was created.

我记得读过不同的定义..

Mozilla 对于他们所说的以下代码也自相矛盾:

function init() {
    var name = "Mozilla"; // name is a local variable created by init
    function displayName() { // displayName() is the inner function, a closure
        alert (name); // displayName() uses variable declared in the parent function    
    }
    displayName();    
}
init();

init() creates a local variable name and then a function called displayName() displayName() is the inner function (a closure) — it is defined inside init(), and only available within the body of that function.

总而言之,他们说内部函数和外部函数都变成了闭包。

抱歉,问题已关闭。我将“myFunc”误读为“makeFunc”-_-

最佳答案

我认为您误读了文档 - myFunc 是闭包,而不是 makeFunc,正如您所想的那样。:

init() creates a local variable name and then a function called displayName(). displayName() is the inner function (a closure) — it is defined inside init(), and only available within the body of that function . Unlike init(), displayName() has no local variables of its own, and instead reuses the variable name declared in the parent function.

只需将 init 替换为 makeFunc 并将上面的引用应用到您的代码中即可。 displayName 是返回的内容,因此被公开。它是一个可以访问var name的函数对象。
所以,是的,displayName 是一个闭包,它是一个由 makeFunc 创建并返回的闭包。

当您将返回的闭包分配给一个变量时,该变量就是该闭包。在您的情况下, myFunc 被分配了 createFunc 的返回值,这意味着它被分配了一个函数实例,其内部名为 displayName ,该实例具有访问 var name
下次调用 createFunc 时,将创建一个 new name var,并创建一个新的 displayName 函数。如果将其分配给另一个 var,那么您将拥有 2 个闭包,即使它们在内部使用相同的变量名,并且它们都由同一函数返回,但它们是 2 个独立的闭包。

<小时/>

在 JS 中,函数是一等对象(意味着它们可以像任何其他值一样传递和赋值)。这意味着:

var foo = function (bar) //assign function to variable foo
{
    return function()//this function will return a function
    {
        bar();//that invokes bar, an argument passed to the outer function
        alert('bar was invoked');
    };
}
var closure = foo(function(){ alert('test');});//pass function to foo
//returns the inner function, that invokes the function we just passed:
closure();//will alert "test", and then "bar was invoked"
var closure2 = foo(function(){ alert('a new closure');});
closure2();//alerts "a new closure" and "bar was invoked"
closure();// still alerts the same

关于javascript - 哪个函数是真正的闭包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24280243/

相关文章:

javascript - 检测与图像和 Canvas 上的对象的碰撞

javascript - 如何从非标记文本中获取最后几句的文本?

javascript - Angular 工厂访问私有(private)函数

grails - 在 Grails Controller 中重用 Criteria

javascript - 如何在没有 jquery 的情况下向此代码添加淡入和淡出过渡

JavaScript 片段在 Django 模板中不起作用

c++ - 使用 "new"的类作用域

JavaScript 数组 : problems with array declaration and access

php - 如何在PHP中定义一个带参数且不闭包的回调并使用?

javascript - 将 jasmine 测试规范写入 javascript 闭包函数