它的封闭范围有多少(javascript)闭包关闭? [英] Over how much of its enclosing scope does a (javascript) closure close?

查看:123
本文介绍了它的封闭范围有多少(javascript)闭包关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我有一些函数使用它的封闭范围中的变量并在该范围之外使用该函数时(这些范围),那么这称为闭包。



是否有关于函数必须关闭的封闭范围多少的任何规范? (或者,换句话说,多少绝对需要关闭?)



考虑:

 函数Outer(){
var bigGuy = createSomethingHuge();
var tinyNumber = 42;
return(function(){/ * CONTENTS * /});
}

甚至:

  function Layer1(){
var bigOne = somethingHugePlease();
var Layer2 = function(){
var bigToo = morePlease();
var Layer3 = function(){
var huge = reallyHuge();
var tiny = 42;
return(function(){/ * CONTENTS * /});
};
返回Layer3();
};
返回Layer2();
}

这些变量中的哪一个会返回最后的返回函数?它取决于最终函数的内容( eval ...?)?



我主要感兴趣的是这些情况是否存在某种规范,而不是关于某些特定实现的行为。

b $ b

我主要关注这些情况是否有某种规范

ECMAScript规范没有详细说明这一点。它只是说一个函数关闭整个词汇环境,其中包含所有父级作用域中的所有变量,它们组织在所谓的环境记录中。

然而,它没有指定实现应该如何进行垃圾收集 - 所以引擎必须自己优化闭包 - 当它们推断出某些闭合变量从不需要(引用)时,它们通常会这样做。特别是,如果你确实在闭包中的任何地方使用了 eval ,他们当然不能这样做,并且必须保留一切。


与某些特定实现的行为不太相关。

无论如何,您都希望查看 JavaScript封闭如何收集垃圾使用node.js进行垃圾回收关于关闭,LexicalEnvironment和GC 在JavaScript中运行时如何表示闭包和范围


When I have some function that uses variables from its enclosing scope(s) and use that function outside of this scope (these scopes), then this is called a closure.

Is there any specification about over "how much" of its enclosing scope(s) a function has to close? (Or, put differently, over "how less" it absolutely needs to close?)

Consider:

function Outer() {
  var bigGuy = createSomethingHuge();
  var tinyNumber = 42;
  return (function () { /* CONTENTS */ });
}

Or even:

function Layer1() {
  var bigOne = somethingHugePlease();
  var Layer2 = function() {
    var bigToo = morePlease();
    var Layer3 = function() {
       var huge = reallyHuge();
       var tiny = 42;
       return (function () { /* CONTENTS */ });
    };
    return Layer3();
  };
  return Layer2();
}

Which of these variables does the final returned function close over? Does it depend on the contents of that final function (eval ...?)?

I'm mostly interested into whether there is some kind of specification for these cases, not so much about the behavior of some specific implementation.

解决方案

I'm mostly interested into whether there is some kind of specification for these cases

The ECMAScript specification does not really detail this. It simply says that a function closes over the whole lexical environment which includes all variables in all parent scopes, organised in so-called environment records.

Yet it does not specify how an implementation should do garbage-collection - so engines do have to optimise their closures themselves - and they typically do, when they can deduce that some "closed over" variable is never needed (referenced). Specifically, if you do use eval anywhere in the closure, they cannot do that of course, and have to retain everything.

not so much about the behavior of some specific implementation

Regardless, you'll want to have a look at How JavaScript closures are garbage collected, garbage collection with node.js, About closure, LexicalEnvironment and GC and How are closures and scopes represented at run time in JavaScript

这篇关于它的封闭范围有多少(javascript)闭包关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆