Currently, I have a function defined like:
function a(b,c) {
if (typeof(b) === "undefined") { b = 1; }
if (typeof(c) === "undefined") { c = 2; }
...
}
Before, it set to default if the value was falsy. Changing this breaks the following call:
a(null, 3);
There is no undefined object. How can I pass only the second argument?
There is, actually, an undefined object:
There may be an undefined object normally, but clearly in my environment something has gone awry
A good test for checking the existence of a variable is this:
Thus, your function can be rewritten like this :
This would account for any of
undefined,nullorvoid 0passed as arguments. If you find that repetitive, you can always define a utility function :And this precise test is what Coffee-script does with its existential operator
?. Coffee-script offers default arguments as well, which would make your life even easier.