1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
   |  var LoopImages=function(ImgArr,container){     this.ImgArr=ImgArr;     this.container=container } LoopImages.prototype={          createImg:function(){         console.log('LoopImages createImage function')
      },     changeImage:function(){         console.log('LoopImages changeImage function')     } }
  var SlideLoopImg=function(imgArr,container){          LoopImages.call(this,imgArr,container) } SlideLoopImg.prototype=new LoopImages()
  SlideLoopImg.prototype.changeImage=function(){     console.log('SlideLoopImage changeImage function') }
  var FadeLoopImg=function(imgArr,container,arrow){     LoopImages.call(this,imgArr,container)     this.arrow=arrow } FadeLoopImg.prototype=new LoopImages() FadeLoopImg.prototype.changeImage=function () {     console.log('FadeLoopImage changeImage function') }
 
  | 
 
父类将简单的属性放在构造函数中,将复杂的方法放在原型中,子类通过组合继承或者寄生组合继承将父类的方法和属性继承,子类可以将方法重写
需要让每个继承对象独立拥有一份原型对象,或者创建的实例对象的构造函数比较复杂,或者耗时较长,或者通过创建多个对象实现,此时我们最好不用new关键字去复制这些基类,可以通过对这些对象属性和方法进行复制来实现创建,如果涉及多个对象,我们也可以通过原型模式来实现对新对象的创建。首先要有一个原型模式的对象复制方法
基于已经存在的模板对象克隆新对象的模式
argument[0],arguments[1]…参数1,参数2…表示模板对象
这里对模板引用是浅复制,也可以根据需求深复制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
   | function prototypeExtend(){     var F=function () {  },     args=arguments,     i=0,     len=args.length;     for(;i<len;i++){                  for(var j in args[i]){                          F.prototype[j]=args[i][j]         }     }          return new F() } var penguin=prototypeExtend(     {         speed:20,         swim:function(){             console.log('游泳速度'+this.speed)         }     },     {         run:function(speed){             console.log('跑步速度'+speed)         }     },     {         jump:function(){             console.log('跳跃动作')         }
      }
  ) penguin.swim(); penguin.run(10) penguin.jump()
  |