4

I know the call and apply in javascript but how does exactly the difference between javascript Call and Apply..?? and another thing i found some code use this together like this:

  function doSomething() {
    return Function.prototype.call.apply(Array.prototype.slice, arguments);
}

are is the same as..

Array.prototype.slice.apply(arguments)

Why we want to use call and apply together?

I am Andy
  • 163
  • 6
  • 1
    Already answered here: http://stackoverflow.com/questions/1986896/what-is-the-difference-between-call-and-apply[1] [1]: http://stackoverflow.com/questions/1986896/what-is-the-difference-between-call-and-apply – mez Oct 26 '12 at 13:47
  • You can find the answer here: http://stackoverflow.com/a/13004493/1064253 – Deepak Nov 06 '12 at 19:12

2 Answers2

4

No, it is not the same. Array.prototype.slice.apply(arguments) applies the slice function on the current argument object, while Function.prototype.call.apply(Array.prototype.slice, arguments); calls the slice function on the array which is provided as the first argument.

Maybe things like this will become easier with new EcmaScript syntax. Your doSomething is equivalent to

function doSomething(array, ...)
    array.slice(...); // assuming array is really an array
}

whereas the second is equivalent to

function (...) {
    arguments.slice(); // assuming argument objects are actual arrays
}
Bergi
  • 513,640
  • 108
  • 821
  • 1,164
1

ase the same

Array.prototype.slice.apply(arguments)

yes, i think its the same too..!!

Skin Drow
  • 31
  • 2