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

Can't assign let variables inside of closures #102

Closed
jclem opened this issue Oct 28, 2014 · 10 comments
Closed

Can't assign let variables inside of closures #102

jclem opened this issue Oct 28, 2014 · 10 comments
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@jclem
Copy link

jclem commented Oct 28, 2014

Not really a closure, but this most clearly demonstrates the issue:

(function() {
  let foo = 'bar';

  [true].forEach(function() {
    foo = 'baz';
  });

  console.log(foo);
});

Compiles to:

"use strict";
(function() {
  var _foo = 'bar';

  [true].forEach(function(_foo) {
    return function() {
        _foo = 'baz';
      };
  }(_foo));

  console.log(_foo);
});

Should be more like:

"use strict";
(function() {
  var _foo = 'bar';

  [true].forEach(function() {
    _foo = 'baz';
  });

  console.log(_foo);
});
@sebmck sebmck changed the title Can not use use "let" with a closure Can't assign let variables inside of closures Oct 28, 2014
@UltCombo
Copy link

Any progress on this? IMHO this is a rather serious issue.

Here's a more elaborated test case:

for (let x = 0; true;) {
    (function() {
        x = 1;
    }());
    (function() {
        assert.equal(x, 1);
        x = 2;
    }());
    assert.equal(x, 2);
    break;
}

@sebmck
Copy link
Contributor

sebmck commented Oct 31, 2014

It's a current limitation of the current let scoping transformer (not being able to reassign let variables inside of closures). I haven't gotten around to rewriting it because I've been busy with a code generator rewrite.

@UltCombo
Copy link

Thanks for the amazingly quick reply. =] I've been fiddling with the let transformer a bit and just realized that it will most likely require a huge rewrite.

@UltCombo
Copy link

Just curious, but what are the plans for this (if any)? Use try/catch, or a state machine like Traceur's?

@sebmck
Copy link
Contributor

sebmck commented Oct 31, 2014

Definently not try/catch, probably something similar if not the same as Traceur.

@UltCombo
Copy link

Traceur's generated code looks quite horrible considering break/continue/return in for loops, but then again, nested try/catches aren't anything nice either. Thanks for the lightning-fast replies. =]

@sebmck
Copy link
Contributor

sebmck commented Nov 6, 2014

Fixed when 287cbbb is merged.

@sebmck sebmck closed this as completed Nov 6, 2014
@UltCombo
Copy link

UltCombo commented Nov 6, 2014

yay, nice!

@sebmck
Copy link
Contributor

sebmck commented Nov 9, 2014

Fixed as of 1.11.0

@UltCombo
Copy link

UltCombo commented Nov 9, 2014

Awesome, thanks!

sebmck pushed a commit that referenced this issue Mar 16, 2015
Completed generator should throw correct error after it had error inside.
ramasilveyra pushed a commit to ramasilveyra/babel that referenced this issue Sep 30, 2017
JacopKane pushed a commit to JacopKane/babel that referenced this issue Jan 11, 2018
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jan 19, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Jan 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

3 participants