formatting - In CoffeeScript, how should I format a function call that takes an anonymous function and other arguments? -
here's contrived example i've come with:
fn = (f, a, b, c)-> alert("#{f() + + b + c}") fn((-> "hi"), 1, 2, 3)
i'm wondering what's suggested way format last line? example easy understand, imagine if anonymous function (the (-> "hi")
) multi-line , took multiple arguments. code become ugly , start lisp-like.
fn2 = (f, a, b, c)-> alert("#{f(1, 2) + + b + c}") fn2(((a, b) -> c = + b c), 1, 2, 3)
this can pretty nasty. there way should format code make more readable or best advice name anonymous function?
i notice few similar questions asking how this. difference here i'm asking how format it.
i've seen style used couple of times:
fn2 (a, b) -> + b , 1, 2, 3
for example, in settimeout
calls:
settimeout -> alert '1 second has passed' , 1000
but think in general it's better separate parameter function in variable:
add = (a, b) -> + b fn2 add, 1, 2, 3
or, if it's possible change function definition, make function parameter last one:
fn2 1, 2, 3, (a, b) -> + b
Comments
Post a Comment