Call, apply, and bind all change the direction of this, and the first argument in each of them changes the direction of this
The difference is that the second argument to apply accepts an array, whereas call and bind accept consecutive arguments
var name = 'aaa' var age=20 var obj = { name:'xxx', objAge:this.age, MyFun: function (FM, t) {the console. The log (enclosing the name + 'age', enclosing the age + ', from '+ FM +' to '+ t)}} var db = {name:' yyy, age: 18}Copy the code
obj.objAge; //20 obj.myFun(); // XXX age undefined, from undefined obj. Myfun. call(db); //yyy age 18, from undefined to undefined obj. myfun. apply(db); //yyy age 18, from undefined to undefined obj. myfun.bind (db)(); //yyy age 18, from undefined undefined obj. Myfun. call(db,' 1 ',' 1 '); / / yyy age 18, from chengdu to Shanghai obj. MyFun. Apply (db, [' chengdu ', 'Shanghai']); / / yyy age 18, from chengdu to Shanghai obj. MyFun. Bind (db, 'chengdu', 'Shanghai') (); / / yyy age 18, from chengdu to Shanghai obj. MyFun. Bind (db, [' chengdu ', 'Shanghai']) (); // Yyy age 18, from Chengdu, Shanghai to undefinedCopy the code