new 执行过程

218 阅读1分钟

1.new 构造函数会先在内存中创建一个空的对象{}
2.this会指向刚刚创建的空对象
3.执行构造函数的代码,给this添加属性和方法
4.隐式返回这个this

   function Fun(name,age){
      this.name=name;
      this.age=age;
      //return this;//默认隐式返回,是看不到
   }
   let f1 = new Fun('wly',18);

执行过程

    var this = {};
    this = {
       'name':name,
       'age':age
    }
    return this;