I simplified my code to show the problem. When I use this Coffeescript snippet:
$("<div>")
.text "hi"
.appendTo "body"
I expect it to compile like this:
$("<div>").text("hi").appendTo("body")
What it does instead is:
$("<div>").text("hi".appendTo("body"))
I found out that you can keep the brackets and it works, but I guess it’s not the way you’re supposed to write Coffeescript.
Can anyone tell me how I’m supposed to write it so it compiles to the desired output? Thank you very much.
Parentheses in CoffeeScript are optional, but that doesn’t mean you’re not supposed to use them. Feel free to use them! In some cases, like yours, they’re even required.
To me, the big win with optional parentheses is for anonymous callbacks and other cases where the parentheses would otherwise span several lines, like this:
Which, to me, is superior to
But even if that’s your style, go for it!
So, to summarize, just write:
Edit: Or even
In this particular case, though, you could of course do:
TIMTOWDY 😉