手写一个javaScript中的call方法

51 阅读1分钟

Function.prototype.myCall = function (obj,...rest){
   obj = obj ? Object(obj) : window;
   const fn = Symbol("test");
   obj[fn] = this;
   const res = obj[fn](...rest)
   delete obj[fn]
   return res;
 }
let obj = {name:"小米汽车"}
function add(){
    console.log(this.name)
}
add.myCall(obj)