4

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.

Aniruddha
  • 774
  • 1
  • 7
  • 18
  • A function expressions creates a function object when evaluated, not unlike an object literal creates a plain object when evaluated. Would you expect "objects" to become part of the variable object as well? – Bergi Dec 18 '21 at 12:18
  • @Bergi I did not know about this. After a bit of googling, I found [this blog by kangax](https://kangax.github.io/nfe/), that I think talks about this, which I will surely be reading. It would be really helpful if you could elaborate on the concept as an answer. – Aniruddha Dec 18 '21 at 12:31
  • Btw, realise that the first edition is from 11 years ago. The language has matured a lot since then. I recommend you rather read the second edition :-) – Bergi Dec 18 '21 at 12:36
  • @Bergi, I know it's old and I have been reading the second edition in parallel with this. I was just interested to know how things were before ES5. – Aniruddha Dec 18 '21 at 12:39

3 Answers3

1

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

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • It might help if you made the function expressions anonymous, and if you clarified in your "Expression" example snippet that [the `const foo =` is not part of the function expression](https://stackoverflow.com/a/35785835/1048572) – Bergi Dec 18 '21 at 12:22
1

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

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.

Aniruddha
  • 774
  • 1
  • 7
  • 18
  • No. There's no difference whether a function expression is immediately evaluated, passed as an argument to a function, assigned to a variable, or something else - a function expression does not automatically become part of the variable object. – Bergi Dec 18 '21 at 12:19