-
Notifications
You must be signed in to change notification settings - Fork 2k
IIFE Syntax #4188
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
Labels
Comments
You can already do this with fns = []
for i in [0...10]
fns.push do (i) ->
return ->
return i
console.log fns[0]() # 0
console.log fns[3]() # 3 or even shorter: fns = []
for i in [0...10]
fns.push do (i) -> -> i
console.log fns[0]() # 0
console.log fns[3]() # 3 or even as a one-liner: fns = (for i in [0...10] then do (i) -> -> i)
console.log fns[0]() # 0
console.log fns[3]() # 3 |
Woo! That's perfect, thanks! |
I have always used for i in [0...10] then do (i) ->
# loop body in a closure |
also see #2518 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When making loops, Javascript has a problem with bindings in terms of the iterator (counter) and functors.
This problem exists in CoffeeScript as well.
This problem is overcome by using an IIFE:
And (arguably even uglier) in CoffeeScript:
What would be awesome is to have a syntax for an IIFE function that basically resolves to an expression:
or
The above example (not necessarily a syntactic proposal) uses two parenthesis to indicate a function that should be immediately called using the named arguments pulled from the local scope.
That means the above translates roughly to the second JavaScript example above. I haven't looked at the CoffeeScript parser but I imagine checking for a second set of parens wouldn't be too intensive.
Another idea for the syntax:
Not sure if that last one is possible, though.
If this is well received I could take a whack at a PR.
The text was updated successfully, but these errors were encountered: