Could you please point me to the nice way of skipping optional parameters in JavaScript.
For example, I want to throw away all opt_
parameters here:
goog.net.XhrIo.send(url, opt_callback, opt_method, opt_content, {'Cache-Control': 'no-cache'}, opt_timeoutInterval)
To declare optional function parameters in JavaScript, there are two approaches: Using the Logical OR operator ('||'): In this approach, the optional parameter is Logically ORed with the default value within the body of the function. Note: The optional parameters should always come at the end on the parameter list.
js skip() Method. The skip() method is used to skip the given number of elements from collection and returns the remaining collection elements. Parameters: The collect() method takes one argument that is converted into the collection and then skip() method is applied on it.
In JavaScript, function parameters default to undefined . However, it's often useful to set a different default value.
The thing with optional parameters is, they are BAD because they are unintuitive - meaning they do NOT behave the way you would expect it. Here's why: They break ABI compatibility ! so you can change the default-arguments at one place.
Solution:
goog.net.XhrIo.send(url, undefined, undefined, undefined, {'Cache-Control': 'no-cache'})
You should use undefined
instead of optional parameter you want to skip, because this 100% simulates the default value for optional parameters in JavaScript.
Small example:
myfunc(param); //is equivalent to myfunc(param, undefined, undefined, undefined);
Strong recommendation: use JSON if you have a lot of parameters, and you can have optional parameters in the middle of the parameters list. Look how this is done in jQuery.
The safest bet is undefined
, and should work almost ubiquitously. Ultimately, though, you cannot trick the function being called into thinking you truly omitted a parameter.
If you find yourself leaning towards using null
just because it's shorter, consider declaring a variable named _
as a nice shorthand for undefined
:
(function() { // First line of every script file "use strict"; var _ = undefined; // For shorthand // ... aFunction(a, _, c); // ... })(); // Last line of every script
First, know that:
typeof undefined
evaluates to "undefined"
typeof null
evaluates to "object"
So suppose a function takes an argument that it expects to be of type "number"
. If you provide null
as a value, you're giving it an "object"
. The semantics are off.1
As developers continue to write increasingly robust javascript code, there's an increasing chance that the functions you call explicitly check a parameter's value for undefined
as opposed to the classic if (aParam) {...}
. You'll be on shaky ground if you continue to use null
interchangeably with undefined
just because they both happen to coerce to false
.
Be aware, though, that it is in fact possible for a function to tell if a parameter was actually omitted (versus being set to undefined
):
f(undefined); // Second param omitted function f(a, b) { // Both a and b will evaluate to undefined when used in an expression console.log(a); // undefined console.log(b); // undefined // But... console.log("0" in arguments); // true console.log("1" in arguments); // false }
Footnotes
undefined
also isn't of type "number"
, it's whole job is to be a type that isn't really a type. That's why it's the value assumed by uninitialized variables, and the default return value for functions.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