0

i need to replace method apply to call or the other.

this[aType].apply(this, aParams)

of course, aParams must be an array.

how can i do it?

i'm waiting your opinion

georg
  • 211,518
  • 52
  • 313
  • 390
Alta istar
  • 411
  • 1
  • 6
  • 11
  • 1
    Please explain what you are trying to do in more detail. At this point, I have no idea what you mean by "convert", and I have no idea what you mean by "the other". – A. Vidor Jul 16 '16 at 07:23
  • hey let me get u more clarified way- You want to use call instead of the apply method. right? – Ayan Jul 16 '16 at 07:23
  • 1
    What do you mean you "need" to? If `aParams` is an array then you *need* `.apply()` (unless you want the called function to accept the whole array as a single argument). – nnnnnn Jul 16 '16 at 07:27
  • 1
    What is the point of using `Func#call` instead of `Func#apply` ? – Rayon Jul 16 '16 at 07:37
  • I see that you've clarified that "convert" means "replace" . . . I still don't know what "the other" means. – A. Vidor Jul 16 '16 at 07:47
  • `Function.apply.call(this[aType], this, aParams)` Now you have both :) – Yury Tarabanko Jul 16 '16 at 07:51
  • For Javascript varargs check: [SO Answer](http://stackoverflow.com/questions/1959040/is-it-possible-to-send-a-variable-number-of-arguments-to-a-javascript-function). One way to differentiate the arguments would be to check on `argument` property inside the function... – boxed__l Jul 16 '16 at 08:00

3 Answers3

2

I cant really connect to why you exactly need it, as pointed by other users here. But in case you need it, I would say it`s an interesting problem to solve. And you might be definitely interested in the powers of Spread and rest operators in ES6 module.

Check how you can send an array of parameters using the call function here:

function abc(x, y) {
  console.log(x, y);
}
var a = [1, 4];
abc.call(this, ...a)
Ayan
  • 2,300
  • 1
  • 13
  • 28
1

For .apply should use arguments as array, But in .call using arguments as parameter.

var aParams = ['aParam1','aParam2'];

this[aType].apply(this, aParams);

this[aType].call(this,aParams[0],aParams[1]);

if its n number

for (var i = 0; i < aParams .length; i++) {
  (function(i) {

  }).call(this , aParams[i]);
}
rejo
  • 3,352
  • 5
  • 27
  • 34
  • Your answer could be an improvement for question but it does not answer the question.. – Rayon Jul 16 '16 at 07:33
  • Rayon, answer updated – rejo Jul 16 '16 at 07:35
  • I guess OP is quiet aware of how `apply/call` works.. What he is asking is _How to pass n number(dynamic) of arguments in `call`_ – Rayon Jul 16 '16 at 07:41
  • @Rayon I'm confused by what you just said. Isn't the way to pass `n` arguments to `call` simply to . . . pass `n` arguments to `call`? – A. Vidor Jul 16 '16 at 07:45
  • @this-vidor– OP is asking what he does not know how many arguments are there ? Say I have an array of `n` length and I want to pass each item from array as argument.. – Rayon Jul 16 '16 at 07:47
  • Can we give for loop ,it n arguments , like this, for (var i = 0; i < aParams .length; i++) { (function(i) { }).call(this , aParams[i]); } – rejo Jul 16 '16 at 07:53
0

Pass the arguments to the call function

this[aType].call(this, aParams[0],aParams[1],aParams[2])
Piyush.kapoor
  • 6,715
  • 1
  • 21
  • 21