javascript apply call and bind methods

One of the most typical aspects in javascript is use of `this` keyword. while in Object Oriented Javascript `this` plays most vital role. its good to know the theory before putting `this` keywords in practice because in general we are using it but we don’t know the major role of `this`.

some major aspects of `this` keyword :
1. `this` always refers to an object.
2. `this` refers to an object which is calling the function it contains
3. in global `this` refers to window object or may be undefined.

basic method to create object :
var myblog = {
 name : ‘Ankit’
 company : ‘Qexon Infotech Pvt Ltd’
 personalDetails : function(){
 return this.name + ‘ Works at ‘ + company;
 }
}
when you access 
myblog.personalDetails(); 
output : Ankit Works at Qexon Infotech Pvt Ltd

but suppose now we need to borrow that function :
var companyDetails = myblog.personalDetails;
companyDetails();
hence that will not work because we have assigned `this` keyword to the global context which doesn’t have a name and company property. To deal with such things
there are three methods bind, call and apply methods.

1. bind() method: bind method create a new function which when it is called then its `this` keyword will set to the provided value 
example :
var myblog = {
 name : ‘Ankit’
 company : ‘Qexon Infotech Pvt Ltd’
 personalDetails : function(){
 return this.name + ‘ Works at ‘ + company;
 }
}
var companyDetails = function() {
 return this.personalDetails() + ‘ from last 2 years’;
};
var bind = companyDetails.bind(myblog);
bind(); output should be like : Ankit Works at Qexon Infotech Pvt Ltd from last 2 years.
2. call() and apply() methods: The call() method calls a function with a given `this` value and arguments provided individually and when we want
to call any function immediately then we have to use call and apply methods.
example :
var myblog = {
 name : ‘Ankit’
 company : ‘Qexon Infotech Pvt Ltd’
 personalDetails : function(){
 return this.name + ‘ Works at ‘ + company;
 }
}
var companyDetails = function(position) {
 return this.personalDetails() + ‘ from last 2 years’ + ‘ at position ‘ + position;
};
using apply
var apply = companyDetails.apply(myblog,[‘software developer’]);
apply(); output should be like : Ankit Works at Qexon Infotech Pvt Ltd from last 2 years.
using call
var call = companyDetails.apply(myblog,’software developer’);
call(); output should be like : Ankit Works at Qexon Infotech Pvt Ltd from last 2 years.

Note that the basic difference between apply and call method is apply method contains parameter in array whereas call function contains both 
array and string type.