1.创建多个具有相同界面的对象1.工厂模式functioncreatePerson(name,age){leto=newObject()o.age=ageo.sayName=function(){console.log("this.name")}}letperson1=createPerson("li",29)letperson2=createPerson("zhang",30)缺点:没有解决对象的同意问题,不知道对象属于哪个类型2.构造函数方式functionPerson(name,age){this.age=agethis.sayName=function(){console.log("this.name")}//this.sayName=newFunction(console.log("this.name"))}让person1=newPerson("li",29)letperson2=newPerson("zhang",30)//把方法传给外面的函数Person(name,age){this.age=age}functionsayName=function(){console.log("this.name")}letperson1=newPerson("li",29)letperson2=newPerson("zhang",30)缺点:实例化多个对象时,实际上是重复创建可以向外调用多个相同的方法(sayName()),但这相当于defining一个全局方法,自定义类型不能引用到代码中才能很好的聚集在一起。3.原型模式/*无论如何,只要创建了一个函数,就会按照特定的规则为这个函数创建一个原型属性(指向原型对象)。默认情况下,所有原型对象都会自动获得一个名为constructor的属性,指向关联的构造函数*/functionPerson(){}Person.prototype.name="li"Person.prototype.age=29Person.prototype.sayName=function(){console.log("this.name")}//上面的代码是多余的。Rewritewithobjectliterals//Rewritewithliterals会覆盖默认的contructor属性,导致这个属性丢失,所以手动添加回来Person.prototype={constructor:Person,name:"li",age:29,sayName:function(){console.log("this.name")}}缺点(1)削弱了构造函数传递初始化参数的能力(2)所有实例共享同一个属性。如果属性是引用类型,会相互影响。两个对象之间如何继承1.原型链继承functionSuperType(){this.property=true;}SuperType.prototype.getSuperValue=function(){returnthis.property;}functionSubType(){this.subproperty=false}//继承SuperTypeSubType.prototype=newSuperType();//新建方法SubType.prototype.getSubValue=function(){returnthis.subproperty;}//覆盖原有方法SubType.prototype.getSuperValue=function(){returnfalse;}letinstance=newSubType();缺点和之前使用原型模式构造对象一样,原型类型包含引用值时,对象之间会相互影响。同时,子类在实例化时不能给父类传递参数。2窃取构造函数并结合继承函数SuperType(){this.colors=["red","blue","greed"]}functionSubType(){//InheritSuperTypeSuperType.call(this)}letinstance1=新的SubType();instance1.colors.push("black");console.log(instance1.colors)//"red,blue,green,black"letinstance2=newSubType();console.log(instance2.colors)//"red,blue,green"//窃取构造函数的缺点是它必须在构造函数中定义了父类中的方法,不能复用父类的方法//组合继承函数SuperType(name){this.name=namethis.colors=["red","blue""greed"]}SuperType.prototype.sayName=function(){console.log("this.name")}functionSubType(name,age){//继承SuperTypeSuperType.call(this);//第二次调用SuperType()this.age=age}SubType.prototype=newSuperType();//第一次调用SuperType()SubType.prototype.sayAge=function(){console.log(this.age)}letinstance1=newSubType("li",29);instance1.colors.push("black");console.log(instance1.colors)//"red,blue,green,black"instance1.sayName()//"li"letinstance2=newSubType("zhang",30);console.log(instance2.colors)//"red,blue,green"instance2.sayName()//"zhang"缺点是两次调用父类的构造函数,效率问题3.寄生组合继承函数inheritPrototype(subType,superType){letprototype=object(superType.protoType);//复制父类原型prototype.constructor=subType;subType.prototype=原型e}functionSuperType(name){this.name=namethis.colors=["red","blue","greed"]}SuperType.prototype.sayName=function(){console.log("this.name")}functionSubType(name,age){//继承超类型SuperType.call(this);//第二次调用SuperType()this.age=age}interitPrototype(SubType,SuperType);SubType.prototype.sayAge=function(){console.log(this.age);}基本上避免了前面的缺点类型,是应用程序类型继承的最佳模式
