Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
139
rated 0 times [  145] [ 6]  / answers: 1 / hits: 180809  / 15 Years ago, sat, august 22, 2009, 12:00:00

Is it possible to convert an array in JavaScript into a function argument sequence? Example:



run({ render: [ 10, 20, 200, 200 ] });

function run(calls) {
var app = .... // app is retrieved from storage
for (func in calls) {
// What should happen in the next line?
var args = ....(calls[func]);
app[func](args); // This is equivalent to app.render(10, 20, 200, 200);
}
}

More From » arrays

 Answers
8

Yes. In current versions of JS you can use:



app[func]( ...args );


Users of ES5 and older will need to use the .apply() method:



app[func].apply( this, args );


Read up on these methods at MDN:




[#98849] Wednesday, August 19, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
terrence

Total Points: 120
Total Questions: 115
Total Answers: 87

Location: England
Member since Fri, May 22, 2020
4 Years ago
terrence questions
Sat, Jun 5, 21, 00:00, 3 Years ago
Wed, Jun 17, 20, 00:00, 4 Years ago
;