实现简单的call指向
// 思路:将要改变this指向的方法挂到目标this上执行并返回
Function.prototype.mycall = function (context) {
if (typeof this !== ‘function’) {
throw new TypeError(‘not funciton’)
}
context = context || window
context.fn = this
let arg = […arguments].slice(1)
let result = context.fn(…arg)
delete context.fn
return result
}
作者:pain
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
实现
Function.prototype.mycall = function (context) {
if (typeof this !== ‘function’) {
throw new TypeError(‘not funciton’)
}
context = context || window
context.fn = this
let arg = […arguments].slice(1)
let result = context.fn(…arg)
delete context.fn
return result
}