【问题标题】:How to use an object as a function parameter in JS?如何在 JS 中使用对象作为函数参数?
【发布时间】:2022-01-04 07:15:54
【问题描述】:

我创建了一个函数,它应该返回一个包含用户数据的对象。我希望函数接受一个对象作为参数,并且我希望在函数内部定义这个对象,并将所有值预定义为默认参数,以防它们没有在函数调用中传递。示例代码只有 2 个值,但我需要超过 15 个。我怎样才能实现这样的解决方案?

const userCreator = (name, age) => {
  if (name === undefined) {
    name = 'John'
  }
  if (age === undefined) {
    age = 25
  }
  return {
    name:name,
    age:age
  }
};

【问题讨论】:

  • 欢迎来到 Stack Overflow!请拿起tour(你会得到一个徽章!)并通读help center,尤其是How do I ask a good question?你最好的选择是做你的研究,search关于SO和其他地方的相关主题,并给它去吧。 如果您在进行更多研究和搜索后遇到困难并且无法摆脱困境,请发布minimal reproducible example 展示您的尝试并具体说明您遇到的问题。人们会很乐意提供帮助。
  • 旁注:您可以直接在参数列表中应用这些默认值:const userCreator = (name = "John", age = 25) => { /*...*/ };
  • 这能回答你的问题吗? ES6 Object Destructuring Default Parameters

标签: javascript object default-parameters


【解决方案1】:

...我希望在函数内部定义此对象,并将所有值预定义为默认参数,以防它们在函数调用中未传递。

我想你是在说:

  • 您希望函数接受一个对象
  • 您希望该对象的所有属性都是可选的,并由函数指定默认值

为此,您可以使用解构默认值。但也请参见下文,因为在这种 特定 情况下,您想要返回一个对象,您可能想要使用不同的方法。

但是让我们从没有默认值的基本解构开始,然后添加它们:

const userCreator = ({ name, age }) => {
//                   ^−−−−−−−−−−−^−−−−−−−−−−−−−−−− destructuring
    // ...stuff with `name` and `age` here...
    return {
        name,
        age,
    };
};

要为这些属性添加默认值,请在解构模式中添加它们({},否则参数名称将是):

const userCreator = ({ name = "John", age = 25, }) => {
//                         ^^^^^^^^^−−−−−^^^^^−−−− defaults for the properties
    return {
        name,
        age,
    };
};

如果有很多,最好分成几行:

const userCreator = ({
    name = "John",
    age = 25,
}) => {
    return {
        name,
        age,
    };
};

不过,那个版本仍然希望提供一个对象。如果要允许userCreator()(根本不传参数),需要为object参数添加一个默认参数值:

const userCreator = ({
    name = "John",
    age = 25,
} = {}) => {
//^^^^−−−−−−−−−−−−−−−−−−−−−−−−−−− default for the parameter
    return {
        name,
        age,
    };
};

如果根本没有提供参数,则使用{} 作为默认值,如果没有提供name,则使用"John" 作为默认值,如果没有提供age,则使用25 作为默认值.由于默认 {} 上没有 nameage,因此当您使用 userCreator() 时,它们会被默认。


替代方法:

既然要返回一个对象,你可以直接接受对象参数,然后使用property spread或者Object.assign填写默认值,像这样:

const userDefaults = {
    name: "John",
    age: 25,
};
const userCreator = (template) => {
    const result = {        // The result object we'll return
        ...userDefaults,    // Fill in defaults
        ...template         // Overwrite with any properties from the caller
    };
    // Or with `Object.assign` in older environments without property spread:
    //const result = Object.assign(
    //    {},                 // The result object we'll return
    //    userDefaults,       // Fill in defaults
    //    template            // Overwrite with any properties from the caller
    //);
    return result;
};

属性传播和Object.assign都忽略nullundefined,所以如果根本没有传递对象,template就是undefined,并且

【讨论】:

  • 魔术!太感谢了!我相信财产传播的选择是我一直在寻找的!再次感谢所有详细的解释:)
【解决方案2】:

你应该直接在函数参数中定义默认值:

const userCreator = (name = 'John', age = 25) => {
  return {
    name: name,
    age: age
  }
};

【讨论】:

  • 这是我想要避免的。姓名和年龄只是一个例子。我在这个函数中有超过 15 个值要设置:(
  • 啊,好的。不清楚。
猜你喜欢
  • 2021-12-15
  • 1970-01-01
  • 1970-01-01
  • 2020-07-09
  • 2019-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-07
相关资源
最近更新 更多