Created
July 4, 2012 14:24
-
-
Save hpyhacking/3047622 to your computer and use it in GitHub Desktop.
the do keyword 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
var filename, _fn, _i, _len; | |
_fn = function(filename) { | |
return fs.readFile(filename, function(err, contents) { | |
return compile(filename, contents.toString()); | |
}); | |
}; | |
for (_i = 0, _len = list.length; _i < _len; _i++) { | |
filename = list[_i]; | |
_fn(filename); | |
} |
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
## coffeescript function in loop | |
# this not your expect | |
for filename in list | |
fs.readFile filename, (err, contents) -> | |
compile filename, contents.toString() | |
# bingo !!! | |
for filename in list | |
do (filename) -> | |
fs.readFile filename, (err, contents) -> | |
compile filename, contents.toString() |
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
// javascript function in loop | |
for (var i = 0, n = elements.length; i<n; i++) { | |
var el = elements[i]; | |
el.addEventListener('click', function() { | |
doSomethingWith(i, el); // i, el are not what you expect! | |
}, false); | |
} | |
for (var i = 0, n = elements.length; i<n; i++) { | |
var el = elements[i]; | |
el.addEventListener('click', (function(i, el) { | |
return function() { | |
doSomethingWith(i, el); | |
} | |
})(i, el), false); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment