0

I have a function defined like this:

doSomething(a,b,c){// ...}

And an array defined like this:

var args = [$(‘.p’),‘#ffc’,’10px’];

I want to call doSomething and pass it all the elements of args as parameters.

Something like:

doSomething($(‘.p’), ‘#ffc’, ‘10px’);

Note that I dont want to change doSomething to have an array as arguments.

Any help !

Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
MsCom
  • 11
  • 2
  • Use [`Function#apply`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply) (like `doSomething.apply(null, args)`) or [spread syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax) (like `doSomething(...args)`) – ibrahim mahrir Dec 25 '20 at 17:09
  • Sorry ! But i dont undestand what you mean – MsCom Dec 25 '20 at 17:11
  • MSCom, there are two ways to do exactly what you want, using javascript. @ibrahimmahrir has provided you with those two methods with links to more details. – freedomn-m Dec 25 '20 at 17:15
  • 1
    Ok. I get the point now. I have seen the links. Thanks guys – MsCom Dec 25 '20 at 17:18

0 Answers0