对象作为JavaScript中的函数参数

时间:2019-03-10 12:32:21

标签: javascript function object parameters

如何将对象设置为具有默认值的函数参数

示例:

Response{values=[Value{timestamp=1552215648, val=18.0}, Value{timestamp=123, val=12.24}]}

即使我这样做,也会出现错误

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"
   }
});

2 个答案:

答案 0 :(得分:2)

有两个默认级别:

  1. 在分解对象时将其属性默认为

  2. 为整个对象提供默认设置。

第一个是通过破坏默认值,第二个是通过参数默认值。一个例子可能是证明这一点的最好方法。为了简化起见,我将只用一个参数来做一个,但是您也可以用多个参数来做:

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;
}

当然,如果要要求传入一个对象,请保留默认参数。

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;
}

或者,如果您只想为根本没有给出对象的情况提供默认值,而如果不是,则不要提供默认值:

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;
}

最后:由于解构允许嵌套,因此可以嵌套。重新阅读您的问题,我想知道这是否是您要尝试的事情:

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;
}

答案 1 :(得分:0)

您应该添加一个默认对象。因此,您不必强制拥有所有参数。 如果没有参数,则存在默认值。

for (class value : vector)
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"}
});