最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Setting second optional parameter javascript - Stack Overflow

programmeradmin2浏览0评论

Let's say I have a function

function myfunction(a, b, c) {
  // do stuff
}

What is the optimal way to call this function with only param c?

a = 1;
b = 2;
c = 'hello';
myfunction(c);

I was thinking this:

myfunction(undefined, undefined, c);

But there must be a better way. I was thinking passing an object, but this function is already being used, I can't change the param structure.

Let's say I have a function

function myfunction(a, b, c) {
  // do stuff
}

What is the optimal way to call this function with only param c?

a = 1;
b = 2;
c = 'hello';
myfunction(c);

I was thinking this:

myfunction(undefined, undefined, c);

But there must be a better way. I was thinking passing an object, but this function is already being used, I can't change the param structure.

Share Improve this question edited Jul 10, 2018 at 14:07 Alessio Cantarella 5,2113 gold badges29 silver badges36 bronze badges asked Jul 10, 2018 at 14:02 Martin LarocqueMartin Larocque 1903 silver badges10 bronze badges 15
  • write a wrapper. Also for ocd reasons, please avoid writing "most optimal". – ASDFGerte Commented Jul 10, 2018 at 14:03
  • even if no solution is supplied. ^^^^^^^^^^^^^^^^^^^^^^^^ – Nina Scholz Commented Jul 10, 2018 at 14:04
  • 1 @ASDFGerte Not just optimal. Most optimal. Optimaller. Optimallist. – Dave Newton Commented Jul 10, 2018 at 14:04
  • 1 You can create a new function myfunctionc by curry-in the function myfunction: myfunctionc = c => myfunction(undefined, undefined, c); myfunctionc(c); – nem035 Commented Jul 10, 2018 at 14:05
  • 3 @kshetline because many of the answers are either plain wrong, did not read the question, or have bad and unnecessary side effects. I did not downvote any, but can totally see where it is ing from. – ASDFGerte Commented Jul 10, 2018 at 14:12
 |  Show 10 more ments

5 Answers 5

Reset to default 2

The best method is to define the parameters in the right order, so the last ones are the optionals, and then you will be able to use the optional arguments as defined in ES6

function(c, a=1, b=2) {
   // ...
}

Since named parameters are not supported in JS, if you have many optional parameters, replace them by a single object.

function(obj) {
   // obj.a, obj.b, obj.c
}

Alternatively you could use the arguments object in the function body.

function() {
    // arguments[0], arguments[1], arguments[2]
}

If you are not able to redesign the function, then find out the default values for the parameters, and use those. It is usually 0, an empty array or null.

You can use destructuring like below

const a = 1;
const b = 2;
const c = 'hello'

function myfunction({a,b,c}){
  console.log(c)
}

myfunction({c})


function wrapper({params}){}

Another way is to create a wrapper which will control your parameters (without function modification)

const a = 1;
const b = 2;
const c = 'hello'

function myfunction(a,b,c){
  console.log(c)
}

function wrapperForMyFunction({a, b, c}){
  return myfunction(a,b,c)
}

wrapperForMyFunction({ c })

You can try this (use bind):

a = 1; b = 2; c = 'hello'


function myfunction(a,b,c){
  console.log('a = ', a, 'b = ', b, 'c = ', c);
}

// newFunc same myfunction but first and second parameter is undefined
// potentially incorrect if 'this' is necessary
const newFunc = myfunction.bind(null, undefined, undefined);

newFunc(c);

newFunc(c, 'something');

var a = 1;
var b = 2;
var c = "hello";

myfunction({c:c})

function myfunction(a,b,c){
  if (typeof a === "object") {
      obj = a;
      a = obj.a;
      b = obj.b;
      c = obj.c;
  }
 
  // do stuff
}

this will work but "a" can never be a object, its the only issue

i think you are looking for var args. please take a look.

var a = 1; b = 2; c = 'hello'
function varargsFunction1()  
{  
    console.log(arguments[2]);
}    
varargsFunction1(a,b,c); 
发布评论

评论列表(0)

  1. 暂无评论