简体   繁体   English

对象作为JavaScript中的函数参数

[英]Object as function param in Javascript

How I can set an object as function param with default values 如何将对象设置为具有默认值的函数参数

Example: 例:

function objasparam({obj1: {var1: 0, var2: "something"}, obj2: {...}}) {
   console.log(obj1.var1); //the expacted log is 0 but it is undefined
}
objasparam({
   obj1:{
     var2: "other"
   }
});

even if I do it this way there is an error 即使我这样做,也有错误

function objasparam(obj = {obj1: {var1: 0, var2: "something"}, obj2: {...}}) {
   console.log(obj.obj1.var1); //the expacted log is 0 but it is undefined
}
objasparam({
   obj1:{
     var2: "other"
   }
});

There are two levels for defaults there: 那里有两个默认级别:

  1. Defaulting the properties of the object when destructuring it, 解构对象时默认其属性,

  2. Providing a default for the object as a whole. 为整个对象提供默认值。

The first is via destructuring defaults, the second is via parameter defaults. 第一个是通过破坏默认值,第二个是通过参数默认值。 An example is probably the best way to show this. 一个例子可能是证明这一点的最好方法。 I'll do one with just one parameter to keep it simple, but you can do it with multiple parameters as well: 为了简化起见,我将只用一个参数来做一个,但是您也可以用多个参数来做:

 function objasparam( // Parameter default ------------vvvv {prop1 = 0, prop2 = "something"} = {} // ----^^^--------^^^^^^^^^^^^^---- destructuring defaults ) { console.log(`prop1 = ${prop1}, prop2 = ${prop2}`); } console.log("Calling the function with no parameter at all:"); objasparam(); console.log("Calling it with {prop1: 42}:"); objasparam({prop1: 42}); console.log("Calling it with {prop2: 'answer'}:"); objasparam({prop2: 'answer'}); console.log("Calling it with {prop1: 42, prop2: 'answer'}:"); objasparam({prop1: 42, prop2: 'answer'}); 
 /* Make the console take up the whole result pane */ .as-console-wrapper { max-height: 100% !important; } 

Of course, if you want to require an object gets passed in, leave of the parameter default. 当然,如果要要求传入一个对象,请保留参数default。

 function objasparam( {prop1 = 0, prop2 = "something"} // ----^^^--------^^^^^^^^^^^^^---- destructuring defaults ) { console.log(`prop1 = ${prop1}, prop2 = ${prop2}`); } try { console.log("Calling the function with no parameter at all:"); objasparam(); // Fails because an object is expected } catch (error) { console.error(error); } console.log("Calling it with {prop1: 42}:"); objasparam({prop1: 42}); console.log("Calling it with {prop2: 'answer'}:"); objasparam({prop2: 'answer'}); console.log("Calling it with {prop1: 42, prop2: 'answer'}:"); objasparam({prop1: 42, prop2: 'answer'}); 
 /* Make the console take up the whole result pane */ .as-console-wrapper { max-height: 100% !important; } 

Or if you only want to provide defaults for the case where the object isn't given at all, and not if it is, leave off the destructuring defaults: 或者,如果您只想为根本没有给出对象的情况提供默认值,而如果没有,则不要提供默认值:

 function objasparam( // Parameter default ------------vvvv {prop1, prop2} = {prop1: 0, prop2: "something"} ) { console.log(`prop1 = ${prop1}, prop2 = ${prop2}`); } console.log("Calling the function with no parameter at all:"); objasparam(); console.log("Calling it with {prop1: 42}:"); objasparam({prop1: 42}); console.log("Calling it with {prop2: 'answer'}:"); objasparam({prop2: 'answer'}); console.log("Calling it with {prop1: 42, prop2: 'answer'}:"); objasparam({prop1: 42, prop2: 'answer'}); 
 /* Make the console take up the whole result pane */ .as-console-wrapper { max-height: 100% !important; } 

Finally: This can be nested, since destructuring allows nesting. 最后:这可以嵌套,因为解构允许嵌套。 Re-reading your question, I'm wondering if that's what you were trying to do: 重新阅读您的问题,我想知道这是否是您要尝试的操作:

 function objasparam( { // vvvv--- default for if the object has no `obj1` obj1: {prop1 = 0, prop2 = "something"} = {}, obj2: {prop3 = "three", prop4 = "four"} = {} { // ^^^^--- default for if the object has no `obj2` } = {} // <=== parameter default if nothing is passed ) { console.log(`prop1 = ${prop1}`); console.log(`prop2 = ${prop2}`); console.log(`prop3 = ${prop3}`); console.log(`prop4 = ${prop4}`); } console.log("Calling the function with no parameter at all:"); objasparam(); console.log("Calling it with {obj1: {prop1: 42}}:"); objasparam({obj1: {prop1: 42}}); console.log("Calling it with {obj2: {prop4: 'quattro'}}:"); objasparam({obj2: {prop4: 'quattro'}}); console.log("Calling it with {obj1: {prop1: 42}, obj2: {prop4: 'quattro'}}:"); objasparam({obj1: {prop1: 42}, obj2: {prop4: 'quattro'}}); 
 /* Make the console take up the whole result pane */ .as-console-wrapper { max-height: 100% !important; } 

You should add an default object. 您应该添加一个默认对象。 So you are not forced to have all parameters. 因此,您不必强制拥有所有参数。 If you have no arguments, the defaults exists. 如果没有参数,则存在默认值。

 const objasparam = (config) => { let defaults = { obj1: { var1: 0, var2: "something" }, obj2: { var1: 1 } } config = { ...defaults, ...config } // to get your obj1.var1 console.log(config.obj1.var1); // complete config console.log(config); } objasparam(); objasparam({ obj1: { var1: 22, var2: "another string"} }); 
 .as-console-wrapper { max-height: 100% !important } 

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

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