1. Call //apply is different from call only. The following code does not include apply
Var name = "global scope "; Let obj1 = {name: "obj ", sayName(){return [this.name,...arguments]}} let obj1 = {name: "In" obj1} Function. The prototype. Call = Function () {/ / not the case, this is bound to the window let the object = the arguments [0] | | window; let symbol = Symbol(); Let data = [...arguments].slice(1); //this refers to the function that calls Call. Symbol is used to prevent overwriting object[Symbol] = this due to duplicate attribute names; let result = object[symbol](... data); // delete object[symbol]; return result; } let result = obj.sayName.Call(obj1,"para1","para2"); Console. log(result) ["obj1中", "para1", "para2"] let result1 = obj.sayname.call (); Console. log(result1) [" global scope "]Copy the code
2.bind
Var name = "global scope "; Let obj = {name: "obj ", sayName(... params){ return [this.name,...params] } } let obj1 = { name: "In" obj1} Function. The prototype. The Bind = Function () {/ / not the case, this is bound to the window let the object = the arguments [0] | | window; let that = this; Let data = [...arguments].slice(1); Return function(){let symbol = symbol (); object[symbol] = that; let result = object[symbol](... data.concat(... arguments)); delete object[symbol]; return result; } } let newfunc = obj.sayName.Bind(obj1,"params1","params2","params3"); console.log(newfunc()); / / / "obj1 params1", ""," params2 ", "params3]" the console. The log (newfunc ()); //["obj1中", "params1", "params2", "params3"] let newfunc1 = obj.sayname.bind (); console.log(newfunc1()); //[" global scope "] console.log(newfunc1()); //[" global scope "]Copy the code