简体   繁体   English

为什么执行上下文的变量 object 中没有包含 function 表达式?

[英]Why are function expressions not included in the variable object of the Execution Context?

While going through the Execution Context part of the JavaScript.The Core.通过JavaScript.The Core执行上下文部分 (1st ed.) by Dmitry Soshnikov, I came across this line that explicitly says that function expressions are not included in the variable object property of an execution context. (第 1 版)由 Dmitry Soshnikov 撰写,我遇到了这一行,它明确表示 function 表达式不包含在执行上下文的变量 object 属性中。

A variable object is a container of data associated with the execution context.变量 object 是与执行上下文相关的数据容器。 It's a special object that stores variables and function declarations defined in the context.这是一个特殊的 object 存储变量和在上下文中定义的 function 声明。

Notice, that function expressions (in contrast with function declarations) are not included in the variable object.请注意,变量 object 中不包含 function 表达式(与 function 声明相反)。

I understand that representation of an execution context as objects is an abstract concept and specifics may differ from one case to another, but still, I am interested to know why the author explicitly says that functions expressions are not to be included.我知道将执行上下文表示为对象是一个抽象概念,具体情况可能因情况而异,但我仍然想知道为什么作者明确表示不包括函数表达式。

AFAIK, function expressions are treated as any other variables by JavaScript engines, and I don't see a reason why they should be omitted from variable objects. AFAIK,function 表达式被 JavaScript 引擎视为任何其他变量,我看不出为什么应该从 变量对象中省略它们。

Edit1: As per Quentin's answer , my conception about function expressions being treated as ordinary variables by JS engines(as stated in the para above) was wrong. Edit1:根据Quentin 的回答,我关于 function 表达式被 JS 引擎视为普通变量的概念(如上文所述)是错误的。 But still, the question stands.但是,问题仍然存在。

AFAIK, function expressions are treated as any other variables by JavaScript engines AFAIK,function 表达式被 JavaScript 引擎视为任何其他变量

They aren't.他们不是。

A function expression evaluates to a value which is a function . function 表达式的计算结果为function Unlike a function declaration they do not create a variable as a side effect.与 function 声明不同,它们不会创建变量作为副作用。

Declaration宣言

function bar () { };

This creates a function named (ie with an internal name of) bar and stores it in a variable named bar.这将创建一个名为 bar(即内部名称为)的 function,并将其存储在名为 bar 的变量中。

Expression表达

const foo = function bar() { };

Here const foo creates a variable named foo.这里const foo创建了一个名为 foo 的变量。

The function expression creates a function named bar (again, an internal name) and assigns it to foo . function 表达式创建一个名为 bar 的 function (同样,一个内部名称)并将其分配给foo

No variable bar is created.没有创建变量bar

IIFE IIFE

(function bar() { })();

This immediately-invoked function expression creates a function named bar and invokes it.这个立即调用的 function 表达式创建了一个名为 bar 的 function 并调用它。 There is no variable bar .没有变量bar The function isn't stored anywhere. function 未存储在任何地方。


Note that I'm talking about the score the function is defined in above.请注意,我说的是上面定义的 function 的分数。 Things get a little more complicated inside the function declaration and expressions. function 声明和表达式内部的事情变得有点复杂。

I am interested to know why the author explicitly says that functions expressions are not to be included.我很想知道为什么作者明确说不包括函数表达式。

We don't know, you'd have to ask the author:-)我们不知道,你得问作者:-)

If I had written this definition, I would even have left out functions altogether:如果我写了这个定义,我什至会完全省略函数:

" A variable object is a container of data associated with the execution context. It's a special object that stores variables defined in the context. " "变量 object 是与执行上下文相关的数据容器。它是一个特殊的 object 存储在上下文中定义的变量 "

Then afterwards one can clarify that然后之后可以澄清

" Variables are defined by variable declarations, function declarations, and function parameters. Nothing else creates variables. " "变量由变量声明、function 声明和 function 参数定义。没有其他东西可以创建变量。 "

Sure, you also have to understand that there is a difference between a function declarations and function expressions , and that it depends on the lexical context which of the two a function keyword creates.当然,您还必须了解function 声明和 function 表达式之间存在差异,并且这取决于两个function关键字创建的词汇上下文。 This I would however expect in a section on parsing and expression evaluation, not in the discussion of variable objects.然而,我希望在关于解析和表达式评估的部分中,而不是在变量对象的讨论中。

After a bit of careful reading, I realized that the author had been using the term `function expression' for both what we generally refer to as IIFE and general function expression .经过一番仔细阅读,我意识到作者一直在使用术语“函数表达式”来表示我们通常所说的IIFE和一般的 function 表达式

In another article , the author clarifies that function expressions that are saved to a variable are, in fact, included in the variable object. 在另一篇文章中,作者阐明了保存到变量中的 function 表达式实际上包含在变量 object 中。 It's the IIFEs that are not included, which makes sense.不包括 IIFE,这是有道理的。

So, to sum it up, function expressions are indeed included in the variable object of a given execution context, whereas IIFEs are not.因此,总而言之,function 表达式确实包含在给定执行上下文的变量 object 中,而 IIFE 则没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM