Last active
August 29, 2015 13:57
-
-
Save lmartins/9805829 to your computer and use it in GitHub Desktop.
Quick Self-invokinf functions in Coffeescript
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
#If you need to declare a self calling function in CoffeeScript, it's super easy, just do it: | |
message = "but stay safe" | |
do -> | |
message = "take chances" | |
alert message | |
alert message | |
# Outputs "takes chances" then "but stay safe" | |
# This gets super useful when you need to freeze a variable, like when in a for loop and using setTimeout, just pass in the variable: | |
for person in group | |
do (person) -> | |
setTimeout -> | |
# A bunch of work using person | |
, 0 | |
# Without the call do (person) -> the function called by setTimeout would only use the last person in the group. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment