I was using js2coffee but it doesn’t seem to translate because I get an unexpected identifier.
$.fn.wait = function( ms, callback ) {
return this.each(function() {
window.setTimeout((function( self ) {
return function() {
callback.call( self );
}
}( this )), ms );
});
};
My coffee version:
$.fn.wait = (ms, callback) ->
@each ->
window.setTimeout ((self) ->
->
callback.call self
(this)), ms
First clean up your JavaScript version so that it isn’t trying to be overly clever and difficult to read; the inlined self-executing function really doesn’t do anything for you except make the people maintaining and reading your code hate you:
That’s less noisy and easier to grok at a glance. The
var _this = thistrick is generally replaced by a fat-arrow (=>) in CoffeeScript so we’re left with this:Yes, parentheses are often optional in CoffeeScript but optional and forbidden are different words so I tend to include the parentheses to make the structure easier to see. You could also skip the
fnvariable with something horrific like this:or this:
but throwing an extra variable into the mix is, IMO, much easier on the eyes.
Demos: