7

Possible Duplicate:
What is the difference between call and apply?

What is the main difference between apply and call methods... I go through the web but unable to find the best solution.. Please help me friends...

Community
  • 1
  • 1
Mihir
  • 8,696
  • 18
  • 56
  • 87

3 Answers3

6

Every function in JavaScript receives two objects in addition to the default parameters. These are this and arguments. The value of this is determined by it's invocation pattern. apply or call can be used to invoke a function and provide it a default this object.

This will be very useful in many situations. For example, arguments is an array-like object, but not really an Array with all the useful Array methods. So, to apply an Array method slice on arguments, you can do this:

Array.prototype.slice.apply(arguments, [1, 2])

Had arguments been an object of Array type, you could have used

arguments.slice(1, 2) 

call is nothing but a modified version of apply. See elusive's comment.

Mr.Douglus Crockford gives a very good introduction to JavaScript functions in this video: Function the Ultimate.

dheerosaur
  • 14,736
  • 6
  • 30
  • 31
  • What relevance does the stuff about `arguments` have? – Tim Down Dec 13 '10 at 12:35
  • 2
    OP has been asking questions about JavaScript for quite some time. I assumed that he was getting started with JS. I thought a proper introduction to function invocation pattern would be useful to him. :) – dheerosaur Dec 13 '10 at 12:39
4

The main difference is that call accepts a list of arguments, where arguments after the first one are passed directly to the method:

myFunc.call(thisObj, arg1, arg2, arg3);

Whereas apply accepts only two arguments - the first is the this object and the second is an array of arguments to pass to the method:

myFunc.apply(thisObj, [arg1, arg2, arg3]);

apply is often used in a situation where you want to pass the arguments object, which contains a list of arguments passed to the current function, to another method:

function myOtherFunc(arg1, arg2, arg3) {
    if (typeof arg1 == "object" && arg1 !== null)
        myFunc.apply(this, arguments);
}
Andy E
  • 338,112
  • 86
  • 474
  • 445
  • Thank you and nice to hear.. but can we use any method at any situation or is there any limitations to these mehtods.. – Mihir Dec 13 '10 at 12:12
  • @Mihir: `apply` can be used in place of `call` pretty much anywhere you can think of. However, `call` isn't a great replacement for `apply` because it's not as dynamic. I've updated my answer to give you an example use of `apply`. – Andy E Dec 13 '10 at 12:15
  • 1
    @Mihir: `call` is just syntactic sugar for `apply`. Makes it easier to call functions with just one parameter. – Felix Kling Dec 13 '10 at 12:17
3

.apply() and .call() are very similar. The only difference is how they pass arguments to the called function. .apply() takes an array of arguments, while .call() can be used like a regular function call:

someFunction.apply(context, [argument1, argument2]);

Is equivalent to:

someFunction.call(context, argument1, argument2);
jwueller
  • 30,582
  • 4
  • 66
  • 70
  • Thank you and nice to hear.. but can we use any method at any situation or is there any limitations to these mehtods.. – Mihir Dec 13 '10 at 12:13
  • @Mihir: Both methods are members of the function object prototype. Both can be used on any function. – jwueller Dec 13 '10 at 12:14