-
Notifications
You must be signed in to change notification settings - Fork 2k
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 for self-referencing functions #1188
Comments
Yep, that's how our scoping works. We don't pay attention to variable declaration hoisting rules. Since |
I mean, it isn't really the same as #1121 (though "fixing" that issue would also fix this one). As you say, it's the way JavaScript works:
But more importantly, it feels antithetical to CoffeeScript's ethos to allow people to use the same name to refer to two different variables even on the same line:
I don't see any problem with making a special case for assignments made on the same line as a function definition to be processed for scoping purposes before the inside of the function is. Is there something I'm overlooking? |
I think that suggestion would be even more confusing. At least with the way it is right now, it is very consistent. And how does this differ from #1121? You were asking for variable declaration hoisting. Now you're asking for hoisting only when declaring with an assignment to a function. Remember,
is interpreted in JS as
and you would like CoffeeScript to behave the same way (except only in some certain situations, apparently). That's hoisting. |
Declaring the variable before compiling RHS would fix it, which is reasonable considering
|
@satyr and @TrevorBurnham are right -- this is a bug. I've fixed it in the previous commit by reordering the variable declaration and RHS compilation. |
Someone posted a StackOverflow question this morning that pointed out that
compiles to
That inner
var init;
declaration surprised him, and it surprises me as well. It's a weird exception to normal scoping rules, isn't it? And as a practical matter, enabling functions to overwrite themselves is quite useful.The workaround is simple, of course: Just add
init = null
before the code.The text was updated successfully, but these errors were encountered: