实现一个call函数
Function.prototype.mycall = function (context) {
if (typeof this !== 'function') {
throw new TypeError('非函数')
}
context = context || window;
context.fn = this;
let arg = [...arguments].slice(1)
let result = context.fn(...arg)
delete context.fn
return result
}
var person = {
name: 'person',
getName: function () {
console.log(this.name)
}
}
var boy = {
name: 'boy'
}
person.getName.mycall(boy)
网友评论