I think JSHint is confused. An assignment is an expression and the value of that expression is the assignment's right hand side; that means that f = function() { ... }
is an expression whose value is a function so (f = function() {...})()
is perfectly valid JavaScript.
If you ask JSHint about this:
var f;
(f = 11)();
you'll get the same "Bad invocation" error and we see that JSHint probably isn't inferring the type of f
, it just doesn't want to you (f = x)()
for any x
(even when x
is definitely a function). I'd tell JSHint to go pound sand and find a better tool. However, if you must use JSHint, you can write your CoffeeScript in two pieces:
asyncRecursion = ->
doStuff()
setTimeout asyncRecursion, 1000
asyncRecursion()
and get this JavaScript:
var asyncRecursion;
asyncRecursion = function() {
doStuff();
return setTimeout(asyncRecursion, 1000);
};
asyncRecursion();
which JSHint is happy with. Both your original and the "make JSHint happy" version produce the same result when executed.
For extra fun with JSHint's lack of type inference, ask it what it thinks of this:
var asyncRecursion;
asyncRecursion = 11;
asyncRecursion();