手撸apply函数实现,再也不怕面试题啦!

122 阅读1分钟
Function.prototype.ownApply = function(thisArg,nums=[]){
  var fn = this;
  //判断传入的参数排除0
  var thisArg = (thisArg === 0 || thisArg) ? Object(thisArg) : window
  thisArg.fn = fn
  thisArg.fn(...nums)
}


function foo(num1,num2){
  console.log("foo函数被调用了",this);
  console.log(num1,num2);
}
foo.ownApply(0,[1,20])