简体   繁体   中英

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. (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.

A variable object is a container of data associated with the execution context. It's a special object that stores variables and function declarations defined in the context.

Notice, that function expressions (in contrast with function declarations) are not included in the variable object.

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.

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. But still, the question stands.

AFAIK, function expressions are treated as any other variables by JavaScript engines

They aren't.

A function expression evaluates to a value which is a function . Unlike a function declaration they do not create a variable as a side effect.

Declaration

function bar () { };

This creates a function named (ie with an internal name of) bar and stores it in a variable named bar.

Expression

const foo = function bar() { };

Here const foo creates a variable named foo.

The function expression creates a function named bar (again, an internal name) and assigns it to foo .

No variable bar is created.

IIFE

(function bar() { })();

This immediately-invoked function expression creates a function named bar and invokes it. There is no variable bar . The function isn't stored anywhere.


Note that I'm talking about the score the function is defined in above. Things get a little more complicated inside the function declaration and expressions.

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. "

Then afterwards one can clarify that

" Variables are defined by variable declarations, function declarations, and function parameters. Nothing else creates variables. "

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. 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 .

In another article , the author clarifies that function expressions that are saved to a variable are, in fact, included in the variable object. It's the IIFEs that are not included, which makes sense.

So, to sum it up, function expressions are indeed included in the variable object of a given execution context, whereas IIFEs are not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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