0

The following code demonstrates an immediately invoked function expression used as the right-hand value for a default value in a destructuring assignment expression.

const { foo = (function bar() { return 'bam'; }()) } = {};

Function expression bar is not visible outside of the expression.

Does this mean JavaScript has expression scope?

If so, does this mean that the expression has its own conceptual LexicalEnvironment?

1
  • No. See tc39.github.io/ecma262/… the production AssignmentProperty : IdentifierReference Initializer Commented Nov 30, 2017 at 13:52

2 Answers 2

2

The function expression bar is not visible because it's a function expression, even if a named one. It does not create any symbol for that function in the symbol table.

function bar() {}

This is a function declaration which creates a symbol bar.

(function bar() {});

This is a function expression which does not result in anything. It's a named function expression so you'd see a function name pop up in stack traces and the function can refer to itself by name, but it's not a function declaration.

Sign up to request clarification or add additional context in comments.

2 Comments

So, function expression bar, does not impact the EnvironmentRecord of the outer LexicalEnvironment ("symbol table" in your answer). But given that the function expression will be given a LexicalEnvironment of its own when it is invoked, does that mean JS has expression scope or not? I can't figure it out. I would guess no?
AFAIK there's no such thing as expression scope in Javascript. What you have is simply not any sort of expression that creates any new symbols.
0

Yes and yes.

The official spec for JavaScript even has a section talking about lexical environments.

At a high level, basically any closure or block will define a new level of scope, where functions aren't available outside of them. This is used to help provide scoping and do things like emulate "private" variables.

1 Comment

I do not believe this answer is correct (based on deceze' answer) although I cannot articulate why I think this!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.