原型链与继承(一):六种常见继承模式

一、原型链继承:


  
  
  1. function SuperType(){
  2. this.colors = [ "red", "blue", "green"];
  3. }
  4. SuperType.prototype.Fun = function(){
  5. };
  6. function SubType(){
  7. }
  8. //继承了SuperType
  9. SubType.prototype = new SuperType();
  10. var instance1 = new SubType();
  11. instance1.colors.push( "black");
  12. alert(instance1.colors); //"red,blue,green,black"
  13. var instance2 = new SubType();
  14. alert(instance2.colors); //"red,blue,green,black"

优点:能通过instanceOf和isPrototypeOf的检测

注意:给原型添加方法的语句一定要放在原型替换SubType.prototype = new SuperType();之后

缺点:(1)SuperType中的属性(不是方法)也变成了SubType的prototype中的公用属性,
     如上面例子中的color属性,可以同时被instance1和instance2修改
     (2)创建子类型的时候,不能像父类型的构造函数中传递参数。

二、借用构造函数

  
  
  1. function SuperType(){
  2. this.colors = [ "red", "blue", "green"];
  3. }
  4. function SubType(){
  5. //继承了SuperType
  6. SuperType.call( this);
  7. }
  8. var instance1 = new SubType();
  9. instance1.colors.push( "black");
  10. alert(instance1.colors); //"red,blue,green,black"
  11. var instance2 = new SubType();
  12. alert(instance2.colors); //"red,blue,green"
  13. function SuperType(name){
  14. this.name = name;
  15. }
  16. function SubType(){
  17. //继承了SuperType,同时还传递了参数
  18. SuperType.call( this, "Nicholas");
  19. //实例属性
  20. this.age = 29;
  21. }
  22. var instance = new SubType();
  23. alert(instance.name); //"Nicholas";
  24. alert(instance.age); //29
原理:在子类型构造函数的内部调用超类型构造函数
优点:解决了superType中的私有属性变公有的问题,可以传递参数
缺点:方法在函数中定义,无法得到复用

三、组合继承:

  
  
  1. function SuperType(name){
  2. this.name = name;
  3. this.colors = [ "red", "blue", "green"];
  4. }
  5. SuperType.prototype.sayName = function(){
  6. alert( this.name);
  7. };
  8. function SubType(name, age){
  9. SuperType.call( this, name); //借用构造函数继承属性,二次调用
  10. this.age = age;
  11. }
  12. SubType.prototype = new SuperType(); //借用原型链继承方法,一次调用
  13. SubType.prototype.constructor = SubType;
  14. SubType.prototype.sayAge = function(){
  15. alert( this.age);
  16. };
  17. var instance1 = new SubType( "Nicholas", 29);
  18. instance1.colors.push( "black");
  19. alert(instance1.colors); //"red,blue,green,black"
  20. instance1.sayName(); //"Nicholas";
  21. instance1.sayAge(); //29
  22. var instance2 = new SubType( "Greg", 27);
  23. alert(instance2.colors); //"red,blue,green"
  24. instance2.sayName(); //"Greg";
  25. instance2.sayAge(); //27
优点:继承前两者的优点,能通过instanceOf和isPrototypeOf的检测
缺点:两次调用父构造器函数,浪费内存。

四、原型式继承:

  
  
  1. function object(o){
  2. function F(){}
  3. F.prototype = o;
  4. return new F();
  5. }
使用场合:没必要构建构造函数,仅仅是想模拟一个对象的时候

五、寄生继承:

  
  
  1. function createAnother(original){
  2. var clone = object(original); //通过调用函数创建一个新对象
  3. clone.sayHi = function(){ //以某种方式来增强这个对象
  4. alert( "hi");
  5. };
  6. return clone; //返回这个对象
  7. }
  8. var person = {
  9. name: "Nicholas",
  10. friends: [ "Shelby", "Court", "Van"]
  11. };
  12. var anotherPerson = createAnother(person);
  13. anotherPerson.sayHi(); //"hi"
缺点:方法在函数中定义,无法得到复用

六:寄生组合继承(最理想):

  
  
  1. function inheritPrototype(subType, superType){
  2. var prototype = object(superType.prototype); //创建对象
  3. prototype.constructor = subType; //增强对象
  4. subType.prototype = prototype; //指定对象
  5. }
  6. function SuperType(name){
  7. this.name = name;
  8. this.colors = [ "red", "blue", "green"];
  9. }
  10. SuperType.prototype.sayName = function(){
  11. alert( this.name);
  12. };
  13. function SubType(name, age){
  14. SuperType.call( this, name);
  15. this.age = age;
  16. }
  17. inheritPrototype(SubType, SuperType); //实现继承
  18. SubType.prototype.sayAge = function(){
  19. alert( this.age);
  20. };


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值