Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is my code to call a function using array as parameters:

const x = ['p0', 'p1', 'p2'];

call_me(x[0], x[1], x[2]); // I don't like it

function call_me (param0, param1, param2 ) {

  // ...

}

Can anyone tell me what’s the best way to pass the contents of x into call_me()?

1 Answer

0 votes
by (19.7k points)

Check the code below with uses Function.prototype.apply(): 

const args = ['p0', 'p1', 'p2'];

call_me.apply(this, args);

You can use spread argument, if you have ECMAScript 6: 

call_me(...args);

Interested in Java? Check out this Java Certification by Intellipaat.    

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...