//手写call
Function.prototype.myCall=function(ctx,...args){
//globalThis表示当前环境的全局对象
//Object() 表示包裹args
console.log(ctx)
ctx= ctx===null || ctx===undefined ? globalThis :Object(ctx);
const fn=this;
const key=Symbol(); //用符号的唯一性来产生属性,因为这个key无论怎么写都有可能和传过来的函数名冲突
Object.defineProperty(ctx,key,{
value:fn,
enumerable:false
})
const r=ctx[key](...args);
delete ctx[key];
return r;
}
function fn(a,b,c){
console.log("this",this);
console.log("args",a,b,c)
}
fn.myCall(1,2,3,4)
// console.log("globalThis",globalThis)