实现了一下call和apply
来源:6-5 作用域相关的面试题 - part1

迷你侠
2022-03-02
实现了一下call和apply, 贴出来大家评评,没实现参数校验,只适合正常调用的场景
call
Function.prototype.myCall = function (){
const args = Array.prototype.slice.call(arguments)
const thisObj = args[0];
Object.prototype.runOuterMethod = this;
args.splice(0,1);
thisObj.runOuterMethod(...args);
delete Object.prototype.runOuterMethod
}
apply
Function.prototype.myApply = function (thisObj, args){
Object.prototype.runOuterMethod = this;
thisObj.runOuterMethod(...args);
delete Object.prototype.runOuterMethod
}
写回答
2回答
-
前端祭酒师
2022-05-03
牛的 就是这样 不借助apply 通过挂在原型上 通过调用方的指向 两个关键知识点进行实现
012022-05-03 -
双越
2022-03-02
非常好,有自己的想法。
提个小建议:考虑一下这样实现的代码可读性
012022-03-02
相似问题