题解 | #_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;
      };

全部评论

相关推荐

点赞 评论 收藏
分享
每晚夜里独自颤抖:你cet6就cet6,cet4就cet4,你写个cet证书等是什么意思。专业技能快赶上项目行数,你做的这2个项目哪里能提现你有这么多技能呢
点赞 评论 收藏
分享
龙珠传说:nb,公务员解约不需要支付违约金吧
点赞 评论 收藏
分享
评论
5
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务