Function.prototype.myCall = function(ctx, ...args) {
const glob = (ctx === null || ctx === undefined) ? globalThis : Object(ctx);
let key = Symbol('mySymbol');
Object.defineProperty(glob, key, {
enumerable: false,
value: this,
});
const result = glob[key](...args);
delete glob[key];
return result;
}
function method(a, b) {
console.log(this, a, b);
return a + b;
}
console.log(method.call({},1,2))
console.log(method.myCall({},1,2))
手撕源码之手写call
于 2024-05-14 09:39:42 首次发布