简体   繁体   English

Javascript:将对象解包为函数参数

[英]Javascript: unpack object as function parameters

I have a function, in Javascript我有一个函数,在 Javascript 中

function asd(foo, bar, baz) {
    // ...
}

And each of those 3 arguments is required.这三个参数中的每一个都是必需的。

Suppose I have those arguments grouped as an object:假设我将这些参数分组为一个对象:

var x = {
    foo: 10,
    bar: 20,
    baz: 30
};

Can I call function asd just by giving object x somehow "unpacked"?我可以通过以某种方式“解压缩”对象x调用函数asd吗? Can this be done generically?这可以通用吗? ie, not knowing the signature of asd .即,不知道asd的签名。

I'm searching for something like kwargs unpacking in Python:我正在寻找类似 kwargs 在 Python 中解包的东西:

def asd(foo, bar, baz):
    # ...

kwargs = {'foo': 10, 'bar': 20, 'baz': 30}
asd(**kwargs)

A "no, not possible" answer is acceptable. “不,不可能”的回答是可以接受的。 I just want to know whether this is possible in Javascript.我只想知道这在 Javascript 中是否可行。

This is now possible in JavaScript.这现在在 JavaScript 中是可能的。 The other comment stating that it is not possible natively is not correct (and wasn't at time of posting).另一条评论说这在本地是不可能的是不正确的(并且不是在发布时)。 It was added roughly a year after you asked - adding an answer for people landing here many years later.它是在您提出问题大约一年后添加的 - 为多年后登陆这里的人们添加答案。

Your function simply needs to change to:您的函数只需更改为:

function asd({foo, bar, baz}) {
    // ...
}

and you call it like this:你这样称呼它:

var x = {
    foo: 10,
    bar: 20,
    baz: 30
};
asd(x);

See MDN Destructuring Assignment .请参阅MDN 解构赋值

function asd(foo, bar, baz) {
    console.log(foo, bar, baz);
}

var x = {
    foo: 10,
    bar: 20,
    baz: 30
};

var args = [];
for(var i in x){
    args.push(x[i]);
}
asd.apply(window, args);

But, like other people said, ordering is a problem.但是,正如其他人所说,订购是一个问题。 With for in you get the properties in the order that they were defined.使用 for 可以按照定义的顺序获取属性。 If that's what you want, then this solution can work for you.如果这就是您想要的,那么此解决方案可以为您服务。 But mapping parameters by name is not possible.但是不能按名称映射参数。

Just making things clearer for people coming from Google like me:只是让像我这样来自谷歌的人更清楚:

No, this is not possible natively in JavaScript.不,这在 JavaScript 中是不可能的

You have to implement something custom to achieve this (see the other answer).您必须实现一些自定义来实现这一点(请参阅其他答案)。

Answring to this old post, I implemented this solution that relies on the spread operator and on parsing the function argument names.回答这个旧帖子,我实现了这个依赖于spread运算符和解析函数参数名称的解决方案。 It can be improved to accept also arrow function.还可以改进接受箭头函数。

 Function.prototype.kwargs = function(argsObj){ const rawArgs = this.toLocaleString().match(/\\(.*\\)\\{/); if(!rawArgs){ console.error("args parsing failed"); return; } const argsList = rawArgs[0].replace(/\\(|\\)|\\{/g, '').split(','); const args = argsList.map((param) => argsObj[param.trim()] || param); return this(...args); } function sum(a,b){ return a+b; } const input = {a:1, b:2} console.log(sum.kwargs({a:1, b:2})) function sum2(a12, b){ return a12+b; } const input2 = {a12:1, b:2}; console.log(sum2.kwargs(input2));

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

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