admin管理员组

文章数量:1374798

function myfunc( value1, value2='defaultValue' ) {
  //some stuff
}

How will I know that if a parameter is passed in or not into a function. I know that the default value will be set if you don't pass in anything as argument.I actually want to check if the user is passing anything as a 2nd parameter ( even if it is same as the default value ) and change my return value accordingly.

I was checking out ES6 docs and this SO answer , Not really what I am looking for.

Thanks,

function myfunc( value1, value2='defaultValue' ) {
  //some stuff
}

How will I know that if a parameter is passed in or not into a function. I know that the default value will be set if you don't pass in anything as argument.I actually want to check if the user is passing anything as a 2nd parameter ( even if it is same as the default value ) and change my return value accordingly.

I was checking out ES6 docs and this SO answer , Not really what I am looking for.

Thanks,

Share Improve this question edited May 23, 2017 at 12:30 CommunityBot 11 silver badge asked Nov 24, 2016 at 15:07 P-RADP-RAD 1,3412 gold badges16 silver badges41 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 10

arguments still represents the actual arguments you were sent, so arguments.length will give you that information.

function myfunc( value1, value2='defaultValue' ) {
  console.log("value1", value1);
  console.log("value2", value2);
  console.log("length", arguments.length);
}
myfunc("foo");

On a pliant JavaScript engine, that outputs

value1 foo
value2 defaultValue
length 1

arguments will not work in an arrow function, though, and estus posted a good alternative that does.

For plex parameter processing rest parameter may be beneficial, also deprecates arguments.length:

function myfunc(...args) {
  let [value1, value2 = 'defaultValue'] = args;

  console.log("value1", value1);
  console.log("value2", value2);
  console.log("args.length", args.length);
}
myfunc("foo");

Manual arguments parsing is particularly useful for the cases with intuitive approach to defaults, i.e. when passed argument should replace default value even if it is undefined:

function myfunc(...args) {
  const defaultArgs = ['defaultValue1', 'defaultValue2'];
  let [value1, value2] = Object.assign(defaultArgs, args);
  ...
}

You can use an unforgeable token as the default argument value. No caller will ever be able to pass this value as an argument, so'defaultValue' you know that the only way that the parameter can end up being bound to this particular value is that no argument was passed.

Thankfully, ECMAScript has a datatype for unforgeable tokens that does exactly what we want: Symbol:

const myfunc = (() => {
    const value2Token = Symbol("value2");
    return function myfunc(value1, value2 = value2Token) {
        if (value2 === value2Token) { value2 = 'defaultValue'; }
        // some stuff
    }
})();

This is of course somewhat ugly, but I would contend that it is less ugly than messing with arguments, and it is a sure way of knowing exactly whether an optional argument was passed or not, since Symbols are pletely unforgeable.

本文标签: javascriptES6 with default argumentsWhether a parameter is passed in or notStack Overflow