简体   繁体   English

如何为通过JavaScript中的对象文字模拟的命名函数参数设置默认值?

[英]How to set default values for named function parameters simulated via object literals in JavaScript?

I'd like to use named parameters (simulated via object literals) in my JavaScript functions for ease of readability. 为了方便阅读,我想在JavaScript函数中使用命名参数(通过对象文字进行模拟)。 The code I've written works fine when I explicitly pass my parameters, but I'm having trouble trying to set a default value, so that my function can be called not only via as myFunction({someBoolean:true}) OR as myFunction() which would imply a default of someBoolean:false. 当我显式传递参数时,我编写的代码可以正常工作,但是尝试设置默认值时遇到了麻烦,因此不仅可以通过myFunction({someBoolean:true})或myFunction来调用我的函数()表示默认为someBoolean:false。

myFunction: function({myArgs}){
    myArgs.someBoolean = myArgs.someBoolean || false;
    if (myArgs.someBoolean) {
        [do stuff]
    }
    [do other stuff]
},

This works fine when I call myFunction({someBoolean:true}), but when I call myFunction(), I get "Uncaught TypeError: Cannot read property 'someBoolean' of undefined." 当我调用myFunction({someBoolean:true})时,此方法工作正常,但是当我调用myFunction()时,出现“未捕获的TypeError:无法读取未定义的属性'someBoolean'”。

Thanks in advance! 提前致谢!

Note the previous answer wouldn't work if you wanted multiple default arguments and some were provided: 请注意,如果您想要多个默认参数,并且提供了一些默认参数,则先前的答案将不起作用:

Doing myFunction({otherArg: 'hello'}) wouldn't necessarily set myArgs.someBoolean to false. 进行myFunction({otherArg: 'hello'})不一定myArgs.someBoolean设置为false。

Rather you should change it the below change: 而是应在以下更改中对其进行更改:

myFunction: function(myArgs){ // Switch to regular argument vs {myArgs}
    myArgs = myArgs || {}; // Make sure it provided

    myArgs.someBoolean = myArgs.someBoolean || false;
    myArgs.otherArg = myArgs.otherArg || 'defaultValue'
    if (myArgs.someBoolean) {
        [do stuff]
    }
    [do other stuff]
},

Then this could be called with: something.myFunction({someBoolean: true, otherArg: '...'}) 然后可以使用以下方法调用它: something.myFunction({someBoolean: true, otherArg: '...'})

function x({myArgs}){}

is used for object destructuring, such that you can use myArgs as a variable inside the function when an object with the myArgs property is being passed. 用于对象myArgsmyArgs在传递带有myArgs属性的对象时,可以将myArgs用作函数内的变量。 For example: 例如:

function x({myArgs}){
  console.log(myArgs);
}
x({myArgs: 5}); // logs 5

However, you're accessing the boolean like an object property of myArgs ( myArgs.someBoolean ). 但是,您像myArgsmyArgs.someBoolean )的对象属性一样访问布尔值。 Then you should use this instead: 然后,您应该使用它代替:

function x(myArgs = {someBoolean: false}){
  console.log(myArgs.someBoolean);
}
x({someBoolean: true}); // logs true
x(); // logs false

This now matches your case. 现在,这符合您的情况。 If you want to use someBoolean directly, use this: 如果要直接使用someBoolean ,请使用以下命令:

function x({someBoolean} = {someBoolean: false}){
  console.log(someBoolean);
}
x({someBoolean: true}); // logs true
x(); // logs false

Another option is 另一种选择是

function x({someBoolean = false, someOtherBoolean = true} = {}){
  console.log(someBoolean);
}
x({someBoolean: true}); // logs true
x(); // logs false

Reference on MDN . 有关MDN的参考

The proper way would be 正确的方法是

 function f({someBoolean = false} = {}){ console.log(someBoolean) } f(); // false f({}); // false f({someBoolean: true}); // true 

That is, 那是,

  • If no parameter is passed, fallback to {} . 如果未传递任何参数,则退回到{}

  • Destruct the parameter or the fallback, extracting the someBoolean property as a variable. 销毁参数或回退,将someBoolean属性提取为变量。 If there is no such property, fallback to false . 如果没有这样的属性,则回退为false

myFunction: function({someBoolean = false}){
    if (someBoolean) {
        [do stuff]
    }
    [do other stuff]
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM