//===================================apply===========================================
Function.prototype.apply2 = function (target, params) {
if (target == null) {
return this(...params)
}
let symbol = Symbol(this.name)
target.constructor.prototype[symbol] = this
let t;
try {
t = typeof target === 'function' ? target : new target.constructor(target)
} catch (e) {
t = target
}
t[symbol](...params)
delete target.constructor.prototype[symbol]
}
//===================================call===========================================
Function.prototype.call2= function (target, ...params) {
if (target == null) {
return this(...params)
}
let symbol = Symbol(this.name)
target.constructor.prototype[symbol] = this
let t;
try {
t = typeof target === 'function' ? target : new target.constructor(target)
} catch (e) {
t = target
}
t[symbol](...params)
delete target.constructor.prototype[symbol]
}
手写JavaScript的 apply call
于 2022-08-18 18:59:49 首次发布