题解 | #修改 this 指向#

修改 this 指向

http://www.nowcoder.com/practice/a616b3de81b948fda9a92db7e86bd171

手写 bind

function bindThis(f, oTarget) {
  if (typeof f !== "function") throw new TypeError(f + " must be a function");
  let o = Object(oTarget);
  let args = [].slice.call(arguments, 2);
  let bound = function (...boundArgs) {
    let finalArgs = [...args, ...boundArgs];
    if (this instanceof bound) {
      if (f.prototype) {
        bound.prototype = Object.create(f.prototype);
      }
      let res = f.apply(this, args);
      if (typeof res === "object" || typeof res === "function") {
        return res;
      }
      return this;
    } else return f.apply(o, finalArgs);
  };
  return bound;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-11 17:10
什么素质,我请问呢,要掉小珍珠了。。。又憋屈又生气
苍蓝星上艾露:给它们能的,一群dinner牛马挥刀向更弱者罢了。我写的开源求职AI co-pilot工具,优化你的简历,找到你匹配的岗位,定制你的简历,并让你做好面试准备https://github.com/weicanie/prisma-ai
点赞 评论 收藏
分享
07-02 10:39
门头沟学院 Java
Steven267:说点真实的,都要秋招了,还没有实习,早干嘛去了,本来学历就差,现在知道急了,而且你这个简历完全可以写成一页,劣势太大了,建议转测试
点赞 评论 收藏
分享
不亏是提前批,神仙打架,鼠鼠不配了
站队站对牛:现在92都报工艺岗了
投递韶音科技等公司7个岗位
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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