Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scope of var in the block? #24

Open
ljharb opened this issue Nov 30, 2017 · 7 comments
Open

Scope of var in the block? #24

ljharb opened this issue Nov 30, 2017 · 7 comments

Comments

@ljharb
Copy link

ljharb commented Nov 30, 2017

(related to #16 and #21)

var has function scope:

function foo() {
  console.log(bar, baz); // logs 3, 4

  var bar = 3;
  {
    var baz = 4;
  }
}

What happens when this is done inside a block param?:

function foo() {
  console.log(bar, baz); // logs 3, ??

  var bar = 3;
  something {
    var baz = 4;
  }
}

(and how does this change in strict vs sloppy mode?)

@syg
Copy link

syg commented Nov 30, 2017

The proposal as stands radically changes the scoping + hoisting mental model around { } and makes it even more confusing IMO.

@syg
Copy link

syg commented Nov 30, 2017

It also goes much more beyond var, if { } in control-structure-like expressions are suddenly function scopes, at least the following:

  • Changes variable captures
  • Changes performance mental model of closures being allocated
  • Changes top-level function statement hoisting

@samuelgoto
Copy link
Owner

All really good points, thanks for filling. As raised by Mark, would disallowing var inside block params mitigate some of these problems?

@syg
Copy link

syg commented Nov 30, 2017

As raised by Mark, would disallowing var inside block params mitigate some of these problems?

Making vars hoist to the nearest syntactically obvious function as dherman has suggested would work as well.

@samuelgoto
Copy link
Owner

Got it, thanks.

Fair to say that, if some of those are acceptable alternatives, we would still have the remaining challenge of:

Changes performance mental model of closures being allocated

But would address the following:

Changes variable captures
Changes top-level function statement hoisting

@syg
Copy link

syg commented Nov 30, 2017

Indeed. More problematically, with TCP, the implementability of this proposal is prohibitively low. It would require very significant engineering to get correct and fast in current engines. Without TCP, would you still want to push for the feature?

@samuelgoto
Copy link
Owner

Without TCP, would you still want to push for the feature?

As a rule of thumb, no, I don't think I'd want this feature without TCP, but I'd want to look on a case by case basis to understand what the trade-offs are.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants