Javascript 如何将对象作为方法输入参数而不是作为参数数组展开?

Javascript 如何将对象作为方法输入参数而不是作为参数数组展开?,javascript,ecmascript-6,named-parameters,spread-syntax,Javascript,Ecmascript 6,Named Parameters,Spread Syntax,我有一个函数需要multiply参数,还有一个输入对象,它包含与参数字段名相同的键名信息,举个小例子,例如 const input = { firstName: 'first', lastName: 'last', age: 19 } function test(firstName, lastName, age, otherThings) { console.log('firstName: ', firstName): console.log('lastName:

我有一个函数需要multiply参数,还有一个输入对象,它包含与参数字段名相同的键名信息,举个小例子,例如

const input = {
   firstName: 'first',
   lastName: 'last',
   age: 19
}

function test(firstName, lastName, age, otherThings) {
   console.log('firstName: ', firstName):
   console.log('lastName: ', lastName):
   console.log('age: ', age):
}
现在,我必须通过输入对象的点符号来调用它,或者使用排列,它变成一个数组,然后在那里使用索引

// call method 1
test(input.firstName, input.lastName, input.age, 'other');

// call method - I know it's kinda ugly but just an available way
test(...[input][0], ...[input][1], ...[input][2], 'other');
我想知道是否有其他方法可以使用spread运算符的思想,但不是映射到数组中,而是将对象作为flatMap展开,然后自动将它们映射到方法参数字段中,我知道…输入可能不起作用,因为输入是对象而不是数组,所以它不可编辑

// is it possible?
test(...input.someAction?, 'other');

这将有助于当我的输入对象相当大,并且想在不修改方法签名的情况下想出一个聪明的方法来执行它,请注意我不能修改方法签名或实现,我们可以把它看作是一个接口方法,我们只能决定如何在我们的边上执行它< /p> 常量输入={ 名字:'第一', lastName:“last”, 年龄:19 } 函数testfirstName,lastName,age,otherThings{ console.log'firstName:',firstName; console.log'lastName:',lastName; console.log'age:',age; } test.applythis,Object.values输入; 这会有点效果,但是当对象获得更多属性或者以不同的顺序包含它们时,这当然会中断-它不会将属性放入相应参数名的参数中,这是不可能的。要获得正确的解决方案,应更改测试函数以获取选项对象:

function test(options) {
   console.log('firstName: ', options.firstName):
   console.log('lastName: ', options.lastName):
   console.log('age: ', options.age):
}
或者使用分解:

function test({firstName, lastName, age, otherThings}) {
   console.log('firstName: ', firstName):
   console.log('lastName: ', lastName):
   console.log('age: ', age):
}
然后,您可以使用

test(input)
或者也可以用物体展开

test({...input, otherThings: 'other'})

为什么不把对象作为参数呢?也许可以改用Python。更严重的是,你不能在JS中这样做是不可原谅的。@HereticMonkey,因为我不能修改前面提到的方法签名或实现,这可以被认为是一个不可更改的接口。谢谢!这是可行的,不幸的是,我不能修改方法签名或实现,这可以被认为是一个不可更改的接口,我只能在调用方进行更改。然后,您肯定应该使用testinput.firstName、input.lastName、input.age、“other”;。
test({...input, otherThings: 'other'})