题解 | #_call函数#

_call函数

https://www.nowcoder.com/practice/22df1ed71b204a46b00587fdb780b3ab

      Function.prototype._call = function (context, ...args) {
        // 判断context,如果为null或者undefined,直接指向window
        let cxt = context || window;
        // 新建一个唯一的Symbol,避免重复
        let func = Symbol();
        cxt[func] = this;
        args = args ? args : [];
        // 以对象的方式调用func,此时的this为传入需要绑定的this指向
        const res = args.length > 0 ? cxt[func](args) : cxt[func]();
        // 删除方法,避免对全局造成污染
        delete cxt[func];
        return res;
      };

全部评论

相关推荐

投递完美世界等公司6个岗位 >
点赞 评论 收藏
转发
4 收藏 评论
分享
牛客网
牛客企业服务