Created
August 21, 2011 09:07
-
-
Save zeppelin/1160370 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foo = 10 | |
bar = -> | |
((foo)-> | |
foo = 20 | |
console.log foo # => 20 | |
)() | |
console.log foo # => 10 | |
foo = 30 | |
bar() | |
console.log foo # => 30 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You could use the
do
keyword rather than wrapping your function in parentheses:But generally speaking, the use of shadowing is discouraged in CoffeeScript. There's been talk of adding a compiler warning in cases like this where a function argument shadows a variable declared in a surrounding scope. The exception is that shadowing is highly useful in a loop, which is why the
do
keyword was added to the language:(Without the
do
, you'd get4, 4, 4
, since the function passed tosetTimeout
is only invoked after the loop has completed.)