Changing 'this' When Invoking a Function
‘This’ is the key word in JavaScript to refer to the object that invokes a function. Normally a function is bound to an object from which it is being invoked. If the function is a method of an object, that object is responsible for invoking the function. Otherwise, the global object, normally the window object becomes the default object. Sometimes we need to change the object to another object for invoking the function. This is normally required when we want to apply the method of an object to another object.
There are three methods being built in JavaScript to finish the “changing invoking object” task. They are call, apply and bind. Let’s look at them one by one.
- The apply method
apply() is the method of any functions. It takes two parmeters, the object to be used as the function context, and an array of values to be used as the invocation arguments.Talk is cheap. So let me show you by a few lines of codes how apply works.
- The call method
The call method is very similar to the apply method, except that it takes a list of arguments directly instead of an array of arguments.
- The bind method
The bind method is different from call and apply by returning a function binded to the invoking context as the first parameter of its arguments and passing parameters as its other arguments.
The bind, call and apply are all the methods that are built-in methods for all the functions. They also tell us that functions are first-class objects in JavaScript.