JS change this point of the three methods

1.call()

The first parameter: the this point
, if you want to pass parameters, later followed by parameters, for example:

function fn(x,y){
   console.log(this);  
}
var obj = {
   name:"zs"
}
fn(1,2);
fn.call(obj,1,2);

2.apply()

The first method except that the parameter representing the form of an array

function fn(x,y){
   console.log(this);  
}
var obj = {
   name:"zs"
}
fn(1,2);
fn.apply(obj,[1,2]);

3.bind()

bind only change this point, it does not call the function

function fn(x,y){
   console.log(this);  
}
var obj = {
   name:"zs"
}
fn(1,2);
fn.bind(obj,1,2)();

 

Guess you like

Origin www.cnblogs.com/zzzzzwt/p/11648597.html