1. The call and apply methods change the use of this
On the second argument, call() is passed in as an array and the second argument to apply() as an array. Var obj1 = {name: "sun Wukong ", SWK: function () {console.log(this.name); }} var obj2 = {name: "", ZBJ: function () {console.log(this.name); } } obj1.swk(); // obj2.zbj(); Obj1.swk. Call (obj2); Obj1.swk. Apply (obj2); // apply methodCopy the code
2. The bind method is used to change the use of this
The bind() method creates an instance of the function whose this value is bound to the value passed to bind().Copy the code
function a(y){ return this.x+y; }; var o={x:1}; var g=a.bind(o); g(2); / / 3Copy the code
From this example, we can see that function A is bound to object O and returns a new function g. When g is called, function A is called as a method of object O. The bind() method binds a function to an object and returns a new function whose arguments are passed to the bound function.Copy the code