How to override default parameter in JavaScript - Stack Overflow

I've found lots of creative ways to set default parameters in ES5 & ES6 but I have yet to see

I've found lots of creative ways to set default parameters in ES5 & ES6 but I have yet to see a simple example of how to override default parameters in JavaScript. Take this example:

new Connection(host, port = 25575, password, timeout = 5000)

The default timeout is fine but the port number is not. When calling this function, JavaScript always treats the second parameter as the password parameter:

myConnection = connFactory.getConnection(process.env.IP,
                                         process.env.PORT,
                                         process.env.PASSWORD)

This code results in an authentication error because the second parameter is assumed to be password. How can I override the default parameter for port without modifying the original function definition?

I've found lots of creative ways to set default parameters in ES5 & ES6 but I have yet to see a simple example of how to override default parameters in JavaScript. Take this example:

new Connection(host, port = 25575, password, timeout = 5000)

The default timeout is fine but the port number is not. When calling this function, JavaScript always treats the second parameter as the password parameter:

myConnection = connFactory.getConnection(process.env.IP,
                                         process.env.PORT,
                                         process.env.PASSWORD)

This code results in an authentication error because the second parameter is assumed to be password. How can I override the default parameter for port without modifying the original function definition?

Share Improve this question edited Apr 17, 2018 at 14:31 Cœur 38.8k25 gold badges206 silver badges278 bronze badges asked Dec 20, 2017 at 14:29 EgeeEgee 772 silver badges7 bronze badges 7
  • new Connection(host, port = 25575, password, timeout = 5000) what kind of syntax is this? Can you show us a more plete example of your code? We have no idea what getConnection looks like, so how can we help you with it? – JLRishe Commented Dec 20, 2017 at 14:34
  • Wrap the function in your own function, which allows you to change the order of parameters. – user5734311 Commented Dec 20, 2017 at 14:34
  • 2 This should not be that case. In this fiddle, the default parameter are corretly use and the second parameter is used as port. So I would guess the problem is somewhere else. Please create a minimal example that allows to reproduce the problem. – t.niese Commented Dec 20, 2017 at 14:34
  • how does getConnection look like? It sounds like there is something wrong into getConnection rather than default values – Facundo La Rocca Commented Dec 20, 2017 at 14:36
  • I believe the question is about parameter's ordering rather than overriding default parameter value – The Reason Commented Dec 20, 2017 at 14:36
 |  Show 2 more ments

2 Answers 2

Reset to default 5

You may use a config object as a parameter for your function. For example:

function foo({a='SO', b}) {
  console.log(a, b)
}

foo({b: 'Yeap', a: 'baz'}) // baz Yeap
foo({b: 'foo'}) // SO foo

It will guarantee your ordering.

Assuming that 'getConnection' isn't your function but es from some unreachable for you place, here is one of a few solutions that will guarantee to be working. It is a function which task is to prepare all parameters.

function getConnectionRunner(host, password, optional = {port: 25575, timeout: 5000}) {
  const port = optional.port || 25575;
  const timeout = optional.timeout || 5000;

  connFactory.getConnection(host, port, password, timeout);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745134891a4613152.html

相关推荐

  • How to override default parameter in JavaScript - Stack Overflow

    I've found lots of creative ways to set default parameters in ES5 & ES6 but I have yet to see

    1小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信