wft-utils
Version:
The commonly used tool functions in daily development
17 lines • 494 B
JavaScript
// 手写一个call函数,该方法是要挂载到Function上的原型上,其中this指向调用者,即函数
function myCall() {
const args = Array.from(arguments)
// this的值
let t = args.shift() ?? window
if (t.constructor === Number) {
t = new Number(t)
} else if (t.constructor === String) {
t = new String(t)
} else if (t.constructor === Boolean) {
t = new Boolean(t)
}
t.fn = this
let val = t.fn(...args)
delete t.fn
return val
}