手写new

2022-10-23  本文已影响0人  未路过
   function Student(name, age) {
        this.name = name;
        this.age = age;
      }

      const stu2 = new Student("tom", 18);
      console.log(stu2);

      const stu3 = newFn(Student, "tom1", 181);
      function newFn(fn, ...args) {
        /*     const newObj = {};
        newObj.__proto__ = fn.prototype; */
        const newObj = Object.create(fn.prototype);
        const res = fn.apply(newObj, args);
        if (res instanceof Object) {
          return res;
        } else {
          return newObj;
        }
      }
      console.log(stu3);
上一篇 下一篇

猜你喜欢

热点阅读