gpt4 book ai didi

javascript - JS 函数构造函数每次都重新解析?

转载 作者:行者123 更新时间:2023-11-29 15:40:05 24 4
gpt4 key购买 nike

在 MDN 中,关于 Functions and function scope , parsed every time it is evaluated 是什么意思?这可以通过代码观察到吗?

引用函数构造函数与函数声明与函数表达式部分:

Functions defined by function expressions and function declarations are parsed only once, while those defined by the Function constructor are not. That is, the function body string passed to the Function constructor must be parsed every time it is evaluated. Although a function expression creates a closure every time, the function body is not reparsed, so function expressions are still faster than "new Function(...)". Therefore the Function constructor should be avoided whenever possible.

It should be noted, however, that function expressions and function declarations nested within the function generated by parsing a Function constructor 's string aren't parsed repeatedly. For example:

var foo = (new Function("var bar = \'FOO!\';\nreturn(function() {\n\talert(bar);\n});"))();<br/>
foo(); //The segment "function() {\n\talert(bar);\n}" of the function body string is not re-parsed.

我写了一个代码片段来(尝试)测试和理解它:

var bar = 'FOO!';
var foo = (new Function("return(function() {\n\talert(bar);\n});"))();
bar = 'FOO! again';
foo(); //The segment "function() {\n\talert(bar);\n}" of the function body string is not re-parsed.

var bar2 = 'FOO!2';
var foo2 = function() { alert(bar2); };
bar2 = 'FOO!2 again';
foo2();

两者都提示“again-version”。

reparsed 与否是什么意思?

这可以用代码结果来说明吗?

谢谢。


仅供引用,我尝试了另一个代码片段:

var bar = 'FOO!';
var string1 = "return(function() {\n\talert(bar);\n});";
var foo = (new Function(string1))();
bar = 'FOO! again';
foo(); //The segment "function() {\n\talert(bar);\n}" of the function body string is not re-parsed.
string1 = "return(function() {\n\talert(bar + ' more');\n});";
foo();

两个警报“FOO!又来了”,不是“FOO!又来了”。

最佳答案

他们想要强调的是,每次调用 Function 构造函数时,JS 解析器都需要工作——基本上是显而易见的。没有缓存所涉及的传递的代码字符串。

与闭包相比,这 [仅] 是相关的。假设我们有这两个函数:

function makeAlerterParse(string) {
return Function("alert("+JSON.stringify(string)+");");
}
function makeAlerterClosure(string) {
return function alerter() { alert(string); };
}

这两个函数声明都将在加载脚本时被解析——这并不奇怪。但是,在闭包中,alerter 函数表达式也已被解析。让我们制作一些警报器:

var alerter1 = makeAlerterParser("1"); // Here the parser will be invoked
alerter1(); // no parsing, the function is instantiated already and
alerter1(); // can be interpreted again and again.

var alerter2 = makeAlerterClosure("2"); // Here, no parser invocation -
alerter2(); // it's just a closure whose code was already known
alerter2(); // but that has now a special scope containing the "2" string

还是不惊喜?好,那么你已经明白了一切。警告只是像

这样的显式调用
for (var fnarr=[], i=0; i<100; i++)
fnarr[i] = makeAlerterParse(i);

实际上将调用 100 次 JS 解析器,而闭包版本是免费的。

关于javascript - JS 函数构造函数每次都重新解析?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20695573/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com