Skip to content

Instantly share code, notes, and snippets.

@hpyhacking
Created July 4, 2012 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hpyhacking/3047622 to your computer and use it in GitHub Desktop.
Save hpyhacking/3047622 to your computer and use it in GitHub Desktop.
the do keyword in coffeescript
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);
}
## 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()
// 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