// Apply vs. Call vs. Bind in JavaScript


// Call invokes the function and allows you to pass in arguments one by one.
// Apply invokes the function and allows you to pass in arguments as an array.
// Bind returns a new function, allowing you to pass in a this array and any number of arguments.


// Apply vs. Call vs. Bind Examples

// ------------------------------------------------------

// - Call

var person1 = {firstName: 'Jon', lastName: 'Kuperman'};
var person2 = {firstName: 'Kelly', lastName: 'King'};

function say(greeting) {
    console.log(greeting + ' ' + this.firstName + ' ' + this.lastName);
}

say.call(person1, 'Hello'); // Hello Jon Kuperman
say.call(person2, 'Hello'); // Hello Kelly King

 //--------------------------------------------------------

// - Apply

var person1 = {firstName: 'Jon', lastName: 'Kuperman'};
var person2 = {firstName: 'Kelly', lastName: 'King'};

function say(greeting) {
    console.log(greeting + ' ' + this.firstName + ' ' + this.lastName);
}

say.apply(person1, ['Hello']); // Hello Jon Kuperman
say.apply(person2, ['Hello']); // Hello Kelly King

 //--------------------------------------------------------

// - Bind


var person1 = {firstName: 'Jon', lastName: 'Kuperman'};
var person2 = {firstName: 'Kelly', lastName: 'King'};

function say() {
    console.log('Hello ' + this.firstName + ' ' + this.lastName);
}

var sayHelloJon = say.bind(person1);
var sayHelloKelly = say.bind(person2);

sayHelloJon(); // Hello Jon Kuperman
sayHelloKelly(); // Hello Kelly King

@@ -1499,28 +1499,808 @@
 elly(); // Hello Kelly King%0A
+%0A //--------------------------------------------------------%0A%0A// - When To Use Each%0A%0A// Call and apply are pretty interchangeable. Just decide whether it%E2%80%99s easier to send in an array or%0A// a comma separated list of arguments.%0A// I always remember which one is which by remembering that Call is for comma (separated list) and Apply is for Array.%0A// Bind is a bit different. It returns a new function. Call and Apply execute the current function immediately.%0A// Bind is great for a lot of things. We can use it to curry functions like in the above example.%0A// We can take a simple hello function and turn it into a helloJon or helloKelly.%0A// We can also use it for events like onClick where we don%E2%80%99t know when they%E2%80%99ll be fired but%0A// we know what context we want them to have.%0A%0A%0A%0A%0A%0A

add when to use