function func(x, y) {
this.total = x + y;
console.log(this)
}
var obj = {
name: 'Alibaba'
}
func.call(obj, 100, 300)
~function () {
function callSelf(context, ...args) {
context = (context == null ? window : context)
if (!/^(object|function)$/.test(typeof context)) {
if (/^(symbol|bigint)$/.test(typeof context)) {
context = Object(context)
}
else {
context = new context.constructor(context)
}
}
let key = Symbol('key'), result;
context[key] = this
result = context[key](...args)
delete context[key]
return result
}
Function.prototype.callSelf = callSelf;
}();
func.callSelf(obj, 100, 300)
手写一个call方法
最新推荐文章于 2023-10-20 07:57:33 发布