Function.prototype.myCall = function (context,...args) { let obj = context || window; let func = Symbol() obj[func] = this; args = args ? args : [] const res = args.length > 0 ? objfunc : objfunc; delete obj[func]; return res; }
function add(c, d) { return this.a + this.b + c + d } const obj1 = { a: 1, b: 2 } console.log(add.myCall(obj1, 5, 6))
//手动实现apply Function.prototype.myApply = function (context,args = []) { let obj = context || window; let func = Symbol() obj[func] = this; const res = args.length > 0 ? objfunc : objfunc; delete obj[func]; return res; }
function add(c, d) { return this.a + this.b + c + d } const obj2 = { a: 1, b: 2 } console.log(add.myApply(obj2,[ 5, 6]))