JavaScriptIntermediate#functions#this

Difference between call, apply and bind?

call and apply invoke the function immediately with an explicit this — call takes arguments individually, apply takes them as an array. bind returns a new function with this (and optional arguments) permanently fixed.

Example
function greet(a, b){ return `${this.name}${a}${b}`; }
greet.call({name:'X'}, '!', '?');
greet.apply({name:'X'}, ['!', '?']);
const g = greet.bind({name:'X'});
g('!', '?');

Related Questions

1
JavaScriptIntermediate#functions#es6

Difference between arrow functions and regular functions?

Open
2
JavaScriptIntermediate#prototypes#oop

What is prototypal inheritance?

Open
3
JavaScriptIntermediate#oop#es6

What is the difference between an ES6 class and a constructor function?

Open