0

I've been reading a bunch of web pages that talk about JavaScript interview questions, and I noticed a great many of them say that it's important to know the difference between .call() and .apply().

For example:

http://www.sitepoint.com/5-typical-javascript-interview-exercises/

https://github.com/h5bp/Front-end-Developer-Interview-Questions

For the life of me, I cannot understand why this is considered so important. Does anyone know? To me, it's probably as important as knowing the difference between, say, a js long date and a js short date (that is, it doesn't seem very important to me). My gut instinct tells me someone influential at some point said it's critical to know the difference between .call() and .apply(), and everyone just copied what this person said. But maybe I misunderstand something about .call() and .apply()? I'm not sure.

Ringo
  • 4,086
  • 2
  • 26
  • 40
  • 2
    What is your point. They are different. Different is the way they handle parameters. http://stackoverflow.com/questions/1986896/what-is-the-difference-between-call-and-apply – Daniel Sep 06 '15 at 04:34

1 Answers1

5

Simply put, more advanced types of Javascript will occasionally need to use either .call() or .apply(), particularly code that is trying to proxy functions and pass on arguments or control the value of this.

If you're doing those types of things, then you have to know how both .call() and .apply() work so you can use the correct method (since they don't work identically). If you're not doing those types of advanced things, then a lot of Javascript can be written without using them.

As an interview question, I think it's a reasonable test on whether you understand some of the more advanced nuances of argument passing, calling methods and this handling in Javascript. If you want to do well on those types of questions, you will need to understand both .call() and .apply(), now how to explain what they do and how and when to use them and when you would use one vs. the other.

The whole concept of this and how it is controlled or set in Javascript is often not understood well by lesser experienced Javascript programmers (heck I've even seen some experienced developers that didn't understand it well) so quizzing a candidate on things related to that is a reasonable test and .call() and .apply() are somewhat central to that.

If you were developing library or framework code, you'd be even more likely to be using .call() or .apply() in the work you do.


Here's a basic summary:

Every function in Javascript is an object that has some properties. So, in addition to be able to call a function like func(), you can also reference the properties on it like func.length.

Two of the properties that every function has are .call() and .apply(). They both allow you to call the function in somewhat different ways than just calling it as func().

.call()

We all know, of course, that if you just want to call a function with a fixed number of arguments, that you can just execute func(arg1, arg2) and those arguments will be passed to the function, but what if you want to control what the this value will be when you pass those fixed arguments? Calling func as func(arg1, arg2) will cause the this value inside that function to be set to either to the global object which is window in a browser or if running in strict mode to undefined. Every function call in Javascript such as func(arg1, arg2) resets the this pointer in this way. This is where .call() comes in. You can execute:

func.call(someThisValue, arg1, arg2)

And it will do the same thing as func(arg1, arg2) except it will cause the this pointer to be set to someThisValue.

Note: you can also use .call() with just one argument and it will just set the this pointer and not pass any arguments.

func.call(someThisValue)

.apply()

But, what if your argument list is not fixed and your arguments are in a variable length array or array-like object. You can't really use .call() because you can't type out the right .call() statement for every possible list of arguments. This is where .apply() comes in. A canonical use for .apply() is when you're just trying to pass on the arguments from some other function call. This is common when you're proxying other function calls to slightly modify their behavior, but still call the original and the original either has various forms (so you don't exactly know the arguments that were passed) or lots of different types of function calls all go through this proxy. In this case, you can use .apply(), often with the arguments object. You can see an example of this in the MDN polyfill for .bind().

Let's say I want to hook some existing function named doIt() for logging purposes and doIt() has a number of different ways that it can be called. Using .apply(), I could do it like this:

// regular function already defined in your program
function doIt(arg1, arg2) {
    // do something
}

// then actual usage elsewhere in the program would be just this:
doIt("foo", "bar");

// now install a proxy that can intercept all calls to doIt() and
// add some behavior before and after
(function(origDoIt) {
     // replace doIt function with my own proxy
     doIt = function() {
         console.log("before doIt()");
         // call the original function with all the arguments and this pointer
         // that were passed
         var retVal = origDoIt.apply(this, arguments);
         console.log("after doIt()");
         return retVal;
     }
})(doIt);

FYI, .apply() can also be used to just set the this pointer as in:

func.apply(someThisValue)

In that particular case (and only that case), it works identically to .call().


Here's one of my favorite uses of .apply(). The Math.max() method accepts a variable number of arguments and it will return the largest number of any of those arguments. Thus:

Math.max(1,2,3,4)

will return 4.

But, using .apply(), we can find the max number in any array.

var list = [999,888,777,666,555,444,333,1000];
var m = Math.max.apply(Math, list);
console.log(m);    // 1000

We're using .apply() to send the entire list array as the arguments to Math.max() so it will operate on the entire array.

Note, when ES6 is fully implemented everywhere or in your particular execution envirionment, you can also use the new spread operator to do something similar:

var list = [999,888,777,666,555,444,333,1000];
var m = Math.max(...list);
console.log(m);    // 1000

which becomes kind of a shorthand for what we were doing with .apply()

jfriend00
  • 580,699
  • 78
  • 809
  • 825
  • I think I got the difference of `call` and `apply`. But I still have trouble understanding those immediately invoked functions. Where does the `arguments` variable come from? Is it from the scope around that anonymous function? – wullxz Sep 06 '15 at 05:20
  • @wullxz - `arguments` is present inside every function scope. It is an array-like object that contains all the arguments passed to the function and contains a `.length` property that tells you how many there were. `arguments[0]` is the first argument passed to the function, `arguments[1]`, the second and so on. See [here on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments) for more description. I didn't have to use an IIFE in this case, but it saved my having a global variable to save the `origDoIt` function reference. – jfriend00 Sep 06 '15 at 05:23
  • @wullxz - The `arguments` I'm using in this code is not from the outer IIFE function, it's from the whenever `doIt()` replacement function is called. – jfriend00 Sep 06 '15 at 05:28
  • I'm still not quite sure... would you pass args to this structure by calling this IIFE like this: `(function(origDoIt) { ... })(doit, args)`? – wullxz Sep 06 '15 at 05:36
  • 1
    @wullxz - The IIFE (the outer function) is just a function container to give me a place to store `origDoIt` without using a global variable. It has absolutely nothing to do with the use of `arguments` here at all. I've added an example of how `doIt()` was called. You can do a search on IIFE and find many other references on that (which has nothing to do with `.apply()` or `.call()` in your original question). – jfriend00 Sep 06 '15 at 05:39
  • ah, I think now I got it. The `doIt =...` call inside that IIFE overwrites the `doIt` for the outer scope as well and therefore all future calls to `doIt` call that function that's assigned to `doIt` which contains the additional `console.log`s and the original function call, right? So you'd basically change the behaviour without losing all of its behaviour? – wullxz Sep 06 '15 at 05:43
  • 1
    @wullxz - yes, you got it now. And the `.apply()` is used to pass on the original value of `this` and any arguments passed from the proxy to the original function without having to know exactly what those arguments were or how many there were such that the original function does not know it is being proxied. – jfriend00 Sep 06 '15 at 05:45