1.call的实现
1 Function.prototype.mycall = function (context) { 2 context.fn = this 3 const args = [...arguments].slice(1) 4 const result = context.fn(...args) 5 delete context.fn 6 return result 7 }
2.appyl的实现
1 Function.prototype.myapply = function (context) { 2 context.fn = this 3 const args = arguments[1] ? arguments[1] : [] 4 const result = context.fn(...args) 5 delete context.fn 6 return result 7 }
3.bind的实现
1 Function.prototype.mybind = function (context) { 2 const self = this 3 const args1 = [...arguments].slice(1) 4 return function () { 5 const args2 = [...arguments] 6 console.log([...args1, ...args2]); 7 return self.apply(context, [...args1, ...args2]) 8 } 9 }