在Function的prototype添加myCall和myApply 方法
Function.prototype.mycall = function (ctx,...args){
cxt = ctx||window;
var fn = Symbol();
cxt[fn] = this;
var result = cxt[fn](...args);
return result;
}
var o ={
name:'aaa',
fn:'fn'
};
function callname(a){
console.log(a);
console.log(this.name);
console.log(this.fn);
this.name='bb';
return a;
}
callname.mycall(o,'hhh');
Function.prototype.myapply = function (ctx,args){
cxt = ctx||window;
var fn = Symbol();
cxt[fn] = this;
var result = cxt[fn](...args);
return result;
}
var o ={
name:'aaa',
fn:'fn'
};
function callname(a){
console.log(a);
console.log(this.name);
console.log(this.fn);
this.name='bb';
return a;
}
callname.myapply(o,['hhh']);