Skipping Optional Function Parameters in JavaScript

Skipping optional function parameters in JavaScript

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.

Omit/Skip a parameter with default value while calling a function in JS

You cant ommit a parameter and call it by its name.
Omitting in js would mean to pass undefined so if your post was a 3 argument function you would do

post('http://whatever', undefined,'somebody')

So that the second param takes the default value

However what you can do is take an object as the parameter, destructure and assign default values:

function post({url,params={},body={}}){
}

Then to call the function you would do post({url:'http://whatever',body:'somebody'});

JavaScript function - named parameters issue when skipping an optional parameter

When using the destructuring solution, you must pass an actual object. named_param(3, c=10) simply is invalid syntax (well, it's valid, but it's an assignment to a global c variable that is passed as the second argument - no naming).

function named_param(a, {b=null, c=5}={}) {
console.log("a="+a);
console.log("b="+b);
console.log("c="+c);
}

named_param(3)
named_param(3, {b:"hi"})
named_param(3, {c:10})
named_param(3, {b:"hi", c:10})

Is there a better way to do optional function parameters in JavaScript?

Your logic fails if optionalArg is passed, but evaluates as false - try this as an alternative

if (typeof optionalArg === 'undefined') { optionalArg = 'default'; }

Or an alternative idiom:

optionalArg = (typeof optionalArg === 'undefined') ? 'default' : optionalArg;

Use whichever idiom communicates the intent best to you!

Why can't I skip parameter assignments in a function signature?

Why do I need to provide references for leading parameters in ES6 function expressions?

Because otherwise it would be a syntax error. In not just ES6 but any version of the language you cannot elide formal parameters because the spec does not provide for it.

If you really want to do this (but why?), you could write it as

function ditchFirstArgument(...[, second]) {}

or at least you will be able to in some future version of ES; see https://github.com/tc39/ecma262/commit/d322357e6be95bc4bd3e03f5944a736aac55fa50. This already seems to be supported in Chrome. Meanwhile, the best you can do is

function ditchFirstArgument(...args) {
const [, second] = args;

But why does the spec not allow elision of parameters?

You'd have to ask the people who wrote it, but they may have never even considered it, or if they did, rejected it because it's bug-prone, hardly ever necessary, and can easily be worked around using dummy formal parameters like _.



Related Topics



Leave a reply



Submit