开发者社区 问答 正文

实现一个 new 操作符

实现一个 new 操作符

展开
收起
kun坤 2019-11-28 14:05:26 386 分享 版权
1 条回答
写回答
取消 提交回答
  • function New(func) {
      var res = {};
      if (func.prototype !== null) {
        res.__proto__ = func.prototype;
      }
      var ret = func.apply(res, Array.prototype.slice.call(arguments, 1));
      if ((typeof ret === "object" || typeof ret === "function") && ret !== null) {
        return;
        ret;
      }
      return;
      res;
    }
    var obj = New(A, 1, 2);
    // equals to
    var obj = new A(1, 2);
    
    2019-11-28 14:05:35
    赞同 展开评论
问答地址: