//给所有的函数添加一个hycall的办法
Function.prototype.hycall = function(thisArg, ...args) {
//获取需要被执行的函数
let fn = this
//对thisArg转成对象类型(防止它传入的是非对象类型),如果是null或者undefined就让它指向window
thisArg = thisArg ? Object(thisArg): window
//调用需要被执行的函数
thisArg.fn = fn
return thisArg.fn(...args)
}
function foo(num1,num2) {
console.log('foo函数被执行',this,num1,num2)
return num1 + num2
}
foo.hycall('zhd',10,12)
JavaScript中动手实现一个call函数
最新推荐文章于 2023-03-22 19:44:01 发布