Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
30
rated 0 times [  35] [ 5]  / answers: 1 / hits: 7654  / 10 Years ago, sun, february 23, 2014, 12:00:00

I have a function, in 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? Can this be done generically? i.e., not knowing the signature of asd.



I'm searching for something like kwargs unpacking in 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.


More From » python

 Answers
5

Just making things clearer for people coming from Google like me:


No, this is not possible natively in JavaScript.


You have to implement something custom to achieve this (see the other answer).


[#47487] Friday, February 21, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaelyn

Total Points: 619
Total Questions: 102
Total Answers: 104

Location: Honduras
Member since Sun, Dec 26, 2021
2 Years ago
;