关于new一个对象

个人理解:

  new一个对象的时候,是在对象内创建一个this=this,并且return这个this;

  shou=function(name, qq){

    //let this=new Object();  //new的时候系统自动

    this.name=name;

    //return this;  //new的时候系统自动

  }

  let ar1=new shou('blue','32423534');

  alert(ar1.name)  //blue

摘要  

var a=new F()

这个时候,a就是F的一个实例,注意不要跟继承搞混淆了,这是实例化。此时new有三个作用

1.在构造函数内部声明一个临时对象this
2.在构造函数F中默认返回这个临时对象this,赋给a
3.将临时对象的_proto_指向F的prototype

用更生动的可以看下图

 

只要你在士兵前面使用 new 关键字,那么可以少做四件事情:

 1. 不用创建临时对象,因为 new 会帮你做(你使用「this」就可以访问到临时对象);

 2. 不用绑定原型,因为 new 会帮你做(new 为了知道原型在哪,所以指定原型的名字为 prototype);

 3. 不用 return 临时对象,因为 new 会帮你做;

 4. 不要给原型想名字了,因为 new 指定名字为 prototype。

var 士兵们 = [ ]  ;

for(var i=0; i<100; i++){

  士兵们.push(new 士兵(i))

}

 兵营.批量制造(士兵们)

更全可看:https://zhuanlan.zhihu.com/p/23987456?refer=study-fe

原文地址:https://www.cnblogs.com/luoyuji/p/7976114.html