Skip to content

wrong variable scope when using results of inline loop #3549

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

Closed
socketpair opened this issue Jul 23, 2014 · 4 comments
Closed

wrong variable scope when using results of inline loop #3549

socketpair opened this issue Jul 23, 2014 · 4 comments

Comments

@socketpair
Copy link

result = for value in container
  value

is rendered as

var result, value;
result = (function() {
  var _i, _len, _results;
  _results = [];
  for (_i = 0, _len = container.length; _i < _len; _i++) {
    value = container[_i];
    _results.push(value);
  }
  return _results;
})();

So, why value variable is declared not in enclosed scope, but in "local" scope?

@epidemian
Copy link
Contributor

That IIFE is an implementation detail. The loop variables are scoped to the enclosing function, not the loop itself:

for bar in bars
  foo bar
console.log bar # bar is accessible here; its scope is not restricted to the for block.

A questionable design, but it makes things consistent with variables defined on other block constructs, like if:

if foo
  bar = 42
console.log bar # bar is accessible here; its scope is not restricted to the if block.

@vendethiel
Copy link
Collaborator

Feel free to weigh in here, if you've been bitten by this : #2518

@socketpair
Copy link
Author

No, this is different issue. #2518 is about using value variable INSIDE loop's code. I'm speaking about fact that context is created by coffeescript, but this variable is not put in that context as it suppose to be.

This usage of loop is shoort-cut for creating function that returns array. So why internal variable is visible to others? IMHO it is very wrong.

next, @epidemian, yes, I cleanly understand that original Ecmascript's loop does not define it's own context and so, variable defined inside this loop is really defined in outer function's scope of that loop. But if we speak about CoffeeScript constructions, authors make theris own rules :) intuitive for that language.

update for @epidemian:
I mean just that my case is not the case you shown. sorry for the miunderstanding. I mean that my example show code that intuitively should be exceuted in separate context.

@socketpair
Copy link
Author

x = for y in [1,2,3]
 z = 7

is converted to

var x, y, z;
x = (function() {
  var _i, _len, _ref, _results;
  _ref = [1, 2, 3];
  _results = [];
  for (_i = 0, _len = _ref.length; _i < _len; _i++) {
    y = _ref[_i];
    _results.push(z = 7);
  }
  return _results;
})();

So, now I cleanly understand "everything is value" concept. It really just wrap only resulting value and no more. Even this is cleanly handled if used inside such loop! very good!

So, I close issue. it's not a bug and is invalid feature.

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

No branches or pull requests

3 participants