javascript - Cannot get proper expression -
how can write following javascript coffeescript
foo.bar(function() { dosomething(); })(x, y);
for instance, following doesn't work:
foo.bar -> dosomething() (x, y)
something this:
f -> ... (x, y)
is little ambiguous in coffeescript since (x, y)
valid expression on own. since things of form f(g(x))
, f(g)
more common f(g)(x)
, ambiguity resolved 2 statements:
f -> ...
and
(x, y)
when parser resolves ambiguity in way don't want, solution resolve ambiguity forcing desired interpretation parentheses:
foo.bar(-> dosomething() )(x, y)
that becomes this javascript:
foo.bar(function() { return dosomething(); })(x, y);
that may or may not have same effect javascript you're trying achieve. if foo.bar
cares return value of argument then
return dosomething();
and just
dosomething();
can quite different; implicit "return last expression's value" in coffeescript can trip up. 1 example jquery's each
stop iterating if iterator function returns false
continue if iterator returns nothing @ (i.e. undefined
). if foo.bar
behaves way might want explicitly state foo.bar
's argument doesn't return anything:
foo.bar(-> dosomething() return )(x, y)
that become this javascript:
foo.bar(function() { dosomething(); })(x, y);
and that's you're looking for.
you use named function instead of anonymous one:
pancakes = -> dosomething() foo.bar(pancakes)(x, y)
you still have possible return
problem noted above (and solve same way) perhaps structure easier read , work with; refactor things way if anonymous function gets longer 5-10 lines makes structure easier me eye-ball.
Comments
Post a Comment