实现继承大致可分为两类1.基于构造器工作模式2.基于对象工作模式----------------------A是否使用原型B是否执行属性拷贝C两者都有(原型属性拷贝)1.原型链法Children。prototype=newParent()所属模式1、A
1functionShape(){2this.name="Shape";3this.toString=function(){4returnthis.name;5};6}7functionTwoDShape(){8this.name='2Dshape';9}10functionTriangle(side,height){11this.name='Triangle';12this.side=side;13this.height=height;14this.getArea=function(){15returnthis.side*this.height/2;16};17}18//用构造器Shape()(通过new操作符)另建了一个新的对象,然后用它覆盖TwoDShape构造器的prototype属性。Triangle也一样。19TwoDShape.prototype=newShape();20Triangle.prototype=newTwoDShape();21//对对象prototype属性进行完全替换时,可能会对对象的constructor属性产生一定副作用。因此,对对象constructor属性进行重置是个很好的习惯22TwoDShape.prototype.constructor=TwoDShape;23Triangle.prototype.constructor=Triangle;2425varmy=newTriangle(5,10);26my.getArea();//2527my.toString//'Triangle'2.仅从原型继承法Child.prototype=Parent.prototype;所属模式1B
1functionShape(){2Shape.prototype.name='shape';3Shape.prototype.toString=function(){4returnthis.name;5}6}7functionTwoDShape(){}8TwoDShape.prototype=Shape.prototype;9TwoDShape.prototype.constructor=TwoDShape;//必须在扩展之前完成继承10TwoDShape.prototype.name='2Dshape';11functionTriangle(side,height){12this.side=side;13this.height=height14}15Triangle.prototype=TwoDShape.prototype;16Triangle.prototype.constructor=Triangle;17Triangle.prototype.name='Triangle';18Triangle.prototype.getArea=function(argument){19returnthis.side*this.height/2;20}21varmy=newTriangle(5,10);22my.getArea();//2523my.toString();//'Triangle'24//缺点子对象的修改会影响父对象25vars=newShape();26s.name;//'Triangle'3.临时构造器newF()
所属模式1C
不同于第一种方式,她只会继承于parent的原型属性
1functionextend(Child,Parent){2varF=function(){};3F.prototype=Parent.prototype;4Child.prototype=newF();5Child.prototype.constructor=Child;6Child.uber=Parent.prototype;//通过uber可以访问父对象7}4.原型属性拷贝法所属模式1C
1functionextend2(Child,Parent){2varp=Parent.prototype;3varc=Child.prototype;4for(variinp){5c[i]=p[i]6}7c.uber=p;8}上面4中都是基于构造器的工作模式,下面几种是基于对象的工作模式5.全属性拷贝法(浅属性拷贝法)所属模式2B
1functionextendCopy(p){2varc={};3for(variinp){4c[i]=p[i];5}6c.uber=p;7returnc;8}6.深拷贝法所属模式2B
1functiondeepCopy(p,c){2c=c||{};3for(variinp){4if(p.hasOwnProperty(i)){5if(typeofp[i]==='object'){6c[i]=Array.isArray(p[i][]:);7deepCopy[p[i],c[i]]8}else{9c[i]=p[i]10}11}12}13returnc;14}7.原型继承法所属模式2A
1functionobject(o){2functionF(){}3F.prototype=o;4returnnewF()5}8.扩展与增强模式所属模式2C方式57的混合
1functionobjectPlus(o,stuff){//对象o用来继承对象stuff用于拷贝方法与属性2varn;3functionF(){}4F.prototype=o;5n.uber=o;6for(variinstuff){7n[i]=stuff[i];8}9returnn;10}9.多重继承法所属模式2B按照父对象出现的顺序依次对他们执行属性全拷贝
1functionmulti(){2varn={},stuff,j=0,len=arguments.length;3for(j=0;j 所属模式2A 1functionparasite(victim){//父对象victim2varthat=object(victim);3that.more=1;4returnthat;5}6//例子7varParent={8name:'parent',9num:1010}11functionSon(m){12varthat=newObject(Parent);13that.name='son';14that.getRun=function(){15returnthat.name+'runs'+this.m+'m';16}17}18vart=Son(200);19t.getRun()//sonruns200m20t.num//1011.构造器借用法所属模式基于构造器 1functionParent(id){2this.id=id;3}4functionSon(){5Parent.apply(this,arguments);6}7vart=newSon(100);8t.id//10012.构造器借用与属性拷贝法所属模式基于构造器原型链属性拷贝 1functionChild(){2Parent.appply(this,arguments);3}4extend2(Child,Parent);