This, call, and apply
The point of this:
This always points to an object and to which object is dynamically bound at run time based on the execution environment of the function, not the environment when the function is declared.
1. Methods that are called as objects
When a function is called as a method of an object, this points to the object
2. As a normal function call
This at this point always points to the global object. In the browser's JavaScript, this global object is the Window object.
Sometimes, we want the this div node inside the div node event function to point to it, and you can save a reference to the DIV node with a variable:
var = this;
In ES5 's strict mode, this case is defined as not pointing to the global object, but undefined
3. Constructor invocation
The constructor's appearance is exactly the same as the normal function, and the difference is in the way it is called. When the operator is called with the new operator, the function always returns an object, usually the this in the constructor points to the returned object.
Note: When using new to call the constructor, it is also important to note that if the constructor explicitly returns an object of Type objects, then the result of this operation will eventually return this object, not the one we expected earlier.
4.function.prototype.call or Function.prototype.apply call
Use Function.prototype.call or Function.prototype.apply to dynamically change the this of the incoming function.
The missing this
Call and apply
Function.prototype.call or Function.prototype.apply are a very common method. They function exactly the same, except for the difference in the form of incoming parameters.
Apply accepts two parameters, the first parameter specifies the pointer to the this object in the function body, and the second parameter is an indexed collection, which can be an array or a class, and the Apply method passes the elements in the collection as arguments to the called function:
var function (a,b,c) { alert ([A,b,c]); // output [I/O] };func.apply (null, [I/n]);
Call the number of parameters passed is not fixed, the same as apply, the first parameter is also representative of the function body of this point, starting from the second parameter, each parameter is sequentially passed in the parameter:
var function (a,b,c) { alert ([A,b,c]); // output [I/O] };func.call (null, N/a);
The parameters of JavaScript are internally represented by an array. In this sense, the use of apply is higher than call, we do not have to worry about how many parameters are passed in parameters, only with apply a brain to push the past.
Call is a syntactic sugar wrapped in apply, with a clear idea of how many parameters the function accepts, and also the ability to send parameters by using called.
When using call or apply, if the first parameter we pass in is null, this in the body of the function points to the default host object, which is window in the browser.
However, if it is in strict mode, this is still null in the body of the function.
Use of call and apply:
1. Change the direction of this
The most common use is to change the this point inside the function.
2.function.prototype.bind
Function.prototype.bind, which is used to specify the this point within the function
Simulated BIND implementations:
Function.prototype.bind =function(context) {varSelf = This;//Save original function return function(){//returns a new function returnSelf.apply (context,arguments);//when a new function is executed, the previously passed context is treated as this in the new function body. }};varobj ={name:' Sven '};varFunc =function() {alert ( This. name);//Output: Sven}.bind (obj); func ();
Complex version:
Function. Prototype.bind =function(){ varSelf = This,//Save original functioncontext = [].shift.call (arguements),//the context that needs to be boundargs = [].slice.call (arguements);//remaining parameter to array return function(){ returnself.apply (Context,[].concat.call (Args,[].slice.call (arguements))); //when a new function is executed, the previously passed context is treated as this in the new function body. //and combine two parameters passed in separately as parameters of the new function }};varobj ={name:' Sven '};varFunc=function(a,b,c,d) {alert ( This. name);//Output: Svenalert ([a,b,c,d]);//output: [1,2,3,4]}.bind (obj,1,2); Func (3,4);
3. Ways to borrow other objects
The first scenario for borrowing a method is "borrowing a constructor", which enables some similar effects to be inherited.
The second use scenario of the borrowing method is more closely related to our relationship.
The argument list of a function arguments is a class array object, although it also has "subscript", but it is not a real array, so it cannot be sorted like an array, or add a new element to the collection. In this case, we will often borrow the method on the Array.prototype object. such as Array.prototype.push,array.prototype.slice,array.prototype.shift and so on.
JavaScript Design patterns and development Practices---reading notes (2) This, call, and apply