I'm just learning CoffeeScript and I'm trying to do something I would normally do in plain 'ol JavaScript.
Here's what I tried to do:
initializeWebGL = (canvas) ->
gl = canvas.getContext "webgl" or canvas.getContext "experimental-webgl"
Which compiles to what I kind of expect:
var initializeWebGL;
initializeWebGL = function(canvas) {
var gl;
return gl = canvas.getContext("webgl" || canvas.getContext("experimental-webgl"));
};
In order to get what I really want, I have to wrap the getContext
arguments with parentheses:
initializeWebGL = (canvas) ->
gl = canvas.getContext("webgl") or canvas.getContext("experimental-webgl")
Which produces what I want:
var initializeWebGL;
initializeWebGL = function(canvas) {
var gl;
return gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
};
Is there a better way to do what I'm trying to achieve than to just add parentheses around the function calls like in the second example?
Is there a better way to do what I'm trying to achieve than to just add parentheses around the function calls like in the second example?
No, I don't think so. My rule of thumb is it's OK to omit parentheses when the function call and its arguments are the last thing on a line, otherwise include them.
someFunction 1, 2, 3
someFunction 1, someOtherFunction 2, 3
In general I try to avoid overly-concise, terse statements. They are harder to work with both mentally as well as stepping through in a debugger is trickier.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With