Skipping Optional Function Parameters in JavaScript

Could you please tell me a good way to skip optional parameters in JavaScript.

For example, I want to drop all opt_ options here:

 goog.net.XhrIo.send(url, opt_callback, opt_method, opt_content, {'Cache-Control': 'no-cache'}, opt_timeoutInterval) 
+31
javascript function-parameter
Dec 02 '11 at 12:17
source share
4 answers

Decision:

 goog.net.XhrIo.send(url, undefined, undefined, undefined, {'Cache-Control': 'no-cache'}) 

You should use undefined instead of the optional parameter that you want to skip, because this 100% mimics the default value for optional parameters in JavaScript.

A small example:

 myfunc(param); //is equivalent to myfunc(param, undefined, undefined, undefined); 

Strong recommendation : Use JSON if you have many parameters, and you may have optional parameters in the middle of the parameter list. See how this is done in jQuery .

+65
Dec 02 '11 at 13:22
source share

Short answer

The safest bet is undefined and should work almost everywhere. Ultimately, however, you cannot fool the function being called, assuming that you really omitted the parameter.

If you rely on using null just because it is shorter, consider declaring a variable named _ as beautiful shorthand for undefined :

 (function() { // First line of every script file "use strict"; var _ = undefined; // For shorthand // ... aFunction(a, _, c); // ... })(); // Last line of every script 

More details

First, know that:

  • typeof undefined evaluates to "undefined"
  • typeof null evaluates to "object"

So, suppose the function takes an argument, which is expected to be of type "number" . If you provide null as a value, you give it "object" . Semantics is disabled. one

As developers continue to write more and more reliable javascript code, there is an increasing chance that the functions you call explicitly check the parameter value for undefined , unlike the classic if (aParam) {...} . If you continue to use null interchangeably with undefined just because both of them force to false .

Remember, however, that in fact the function can tell if the parameter was really omitted (compared 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 } 



Footnote

  • As long as undefined also does not belong to the type of "number" , this whole job should be a type that is not a type. That is why the value accepted by uninitialized variables and the default value returned for functions.
+20
Aug 02 2018-12-12T00:
source share

Just pass null as the parameter value.

Added: you can also skip all subsequent optional parameters after the last one that you want to pass the real value (in this case, you can skip the opt_timeoutInterval parameter in opt_timeoutInterval )

+6
Dec 02 '11 at 12:19
source share

What do you mean by a pass? You can go to null , in which case you can completely leave the final parameters ( opt_timeoutInterval ), but you need to make sure that your Cache-Control parameter becomes fifth.

 goog.net.XhrIo.send(url, null, null, null, {'Cache-Control': 'no-cache'}); 

If you do not need Cache-Control:

 goog.net.XhrIo.send(url); 

Other APIs accept parameters as a hash to simplify additional parameters (and many parameters are called, therefore, self-documenting), but where positional parameters are expected, you should go with this.

+1
Dec 02 '11 at 12:22
source share



All Articles