title


手写call函数

1
2
3
4
5
6
7
8
9
10
11
Function.prototype.myCall = function (context,...args) {
var context = context || window;
context.fn=this;
var res=(args!=null&&args!=undefined)?context.fn(...args):context.fn();
delete context.fn;
return res;
}
function test(){
console.log(this);
}
test.myCall({name:'lili'});

手写apply函数

1
2
3
4
5
6
7
Function.prototype.myBind=function(obj,...args){
obj=obj||windows;
fn=this;
return function(params){
fn.apply(obj,params);
}
}

Author: pkq
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source pkq !
  TOC