JS Polyfills — 调用、应用、绑定 |小鬼

JS Polyfills — 调用、应用、绑定 |小鬼

称呼,

Function.prototype.myCall = 函数 (currentContext = {}, ...arg) {

if (typeof this !== 'function') {
throw new Error(this + "it's not callable");
} currentContext.fn = 这个;
currentContext.fn(...arg);

};

申请,

Function.prototype.myApply = 函数 (currentContext = {}, arg = []) {
if (typeof this !== 'function') {
throw new Error(this + "it's not callable");
} 如果(!Array.isArray(arg)){
throw new TypeError('在非对象上调用')
} currentContext.fn = 这个;
currentContext.fn(...arg);

};

绑定,

Function.prototype.myBind = 函数 (currentContext = {}, ...arg) {

if (typeof this !== 'function') {
throw new Error(this + "cannot be bound as it's not callable");
} currentContext.fn = 这个;

返回 功能 () {
返回 currentContext.fn(...arg);
};
};

我知道总会有一些需要改进的地方。请随时分享您的想法

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明

本文链接:https://www.qanswer.top/29376/14211200

posted @ 2022-09-12 00:14  哈哈哈来了啊啊啊  阅读(130)  评论(0)    收藏  举报