The difference between call / apply / bind

Ivan Sifrim
2 min readJan 12, 2018

JavaScript is a dynamic language, and is flexible enough to let you do things like multiple inheritance. That’s when an object or a class can inherit characteristics from more than one parent. This can be done using one of these 3 methods: call / apply / bind. Read below or try it out yourself by cloning the code here.

So what’s the difference?

Call

Let’s say that we have an object called obj. It only has one property called things, which has a value of 3. Totally unrelated to this object, let’s also make a function called addThings.

let obj = {things: 3};let addThings = function(a, b, c){
return this.things + a + b + c;
};

Notice this.things. Why does the addThings function mention that, when it doesn’t even have things? We need to pass it a context. We can do that with call. If we were to run this code:

console.log( addThings.call(obj, 1,4,6) );

It would return the number 14.

That’s because the first parameter that call takes, is the context we want “this” to refer to. We passed it obj, which does have the things property. After we pass the context, we can pass the arguments like we usually would. In this case we passed 1, 4, 6. So this line:

--

--