[Object]面向对象编程(高程版)(五)继承

作者:zccst

一、原型链继承

Js代码  收藏代码
  1. function SuperType(){  
  2.     this.property = true;  
  3. }  
  4. SuperType.prototype.getSuperValue = function(){  
  5.     return this.property;  
  6. }  
  7.   
  8.   
  9. function SubType(){  
  10.     this.subproperty = false;//SubType有了一个属性subproperty,值为false  
  11. }  
  12.   
  13. // 核心  
  14. SubType.prototype = new SuperType();  
  15. //SubType继承了SuperType的属性property,值为true,  
  16. //还继承了SuperType在prototype对象中定义的getSuperValue方法  
  17.   
  18.   
  19. SubType.prototype.getSubValue = function(){  
  20.     return this.subproperty;//SubType又有了一个在prototype对象中定义的getSubValue方法  
  21. }  
  22.   
  23. //自此,SubType拥有4个属性,其中两个是真正的属性,另两个是方法。  
  24.   
  25. //知识点1,重新父类的同名方法。会覆盖父类的同名方法。  
  26. //SubType.prototype.getSuperValue = function(){return false;}  
  27.   
  28. //知识点2,原型链实现继承时,不能使用对象字面量创建原型方法。因为这样做会重写原型链  
  29. SubType.prototype = {  
  30.     saySubValue : function(){return this.subproperty;},  
  31.     sayOtherValue : function(){return false;}  
  32. };  
  33. //批注:运行上面代码时,会使SubType.prototype = new SuperType();无效  
  34.   
  35.   
  36. //console.log(SuperType);  
  37. //console.log(SubType);  
  38.   
  39. var i = new SubType();  //创建一个实例  
  40.   
  41. //alert(i.getSuperValue());  
  42. //alert( i instanceof Object);  
  43. //alert( i instanceof SuperType);  
  44. //alert( i instanceof SubType);  
  45.   
  46. //该实例是三个对象的实例,他们是继承关系。  
  47. //alert(Object.prototype.isPrototypeOf(i));  
  48. //alert(SuperType.prototype.isPrototypeOf(i));  
  49. //alert(SubType.prototype.isPrototypeOf(i));  
  50.   
  51.   
  52. alert(i.getSuperValue());  
  53.   
  54.   
  55. 4,obj4  
  56.   
  57. //1,原型链的问题  
  58. function SuperType(){  
  59.     this.colors = ['red','blue','green'];  
  60. }  
  61. function SubType(){  
  62. }  
  63. SubType.prototype = new SuperType();  
  64.   
  65. var i = new SubType();  
  66. i.colors.push('black');  
  67. alert(i.colors);  
  68. var j = new SubType();  
  69. alert(j.colors);//4。原因是j的prototype与i的一样。里面包含着父类的属性和prototype。  




二、借用构造函数

Js代码  收藏代码
  1. function SuperType(){  
  2.     this.colors = ["red","blue","green"];  
  3. }  
  4.   
  5. function SubType(){  
  6.     SuperType.call(this);//call相当于设置函数体内this对象的值。  
  7. }  
  8.   
  9. var i = new SubType();  
  10. i.colors.push("black");  
  11. alert(i.colors); //"red","blue","green","black"  
  12.   
  13. var i2 = new SubType();  
  14. alert(i2.colors); //"red","blue","green"  
  15.   
  16. //批注:通过call借调了父类的构造函数,实际上是在子类创建实例时调用了父类的构造函数。  
  17.   
  18.   
  19. //1,传递参数  
  20. function SuperType(name){  
  21.     this.name = name;  
  22. }  
  23. function SubType(){  
  24.     SuperType.call(this, "Nicholas");  
  25.     this.age = 29;  
  26. }  
  27.   
  28. var i = new SubType();  
  29. alert(i.name);  
  30. alert(i.age);  
  31.   
  32. //2,问题:方法都在构造函数中,无法复用。  






三、组合继承

Js代码  收藏代码
  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.   
  9. function SubType(name,age){  
  10.     SuperType.call(this,name);  
  11.     this.age = age;  
  12. }  
  13.   
  14. SubType.prototype = new SuperType();  
  15.   
  16. SubType.prototype.sayAge = function(){  
  17.     alert(this.age);  
  18. }  
  19.   
  20. var i = new SubType("nich",29);  
  21. i.colors.push("black");  
  22. //alert(i.colors);//"red","blue","green","black"  
  23. //i.sayName(); //"nich"  
  24. //i.sayAge();  //29  
  25.   
  26. var i2 = new SubType("greg",27);  
  27. //alert(i2.colors);//"red","blue","green"  
  28. //i2.sayName(); //"greg"  
  29. //i2.sayAge();  //27  
  30.   
  31. alert(i.name == i2.name);    //false  
  32. alert(i.age == i2.age);      //false  
  33. alert(i.colors == i2.colors);//false  
  34.   
  35. alert(i.sayName == i2.sayName);//true 原因:sayName也是原型对象中定义的方法  
  36. alert(i.sayAge == i2.sayAge);  //true  



//批注:该方法在实际中是使用最多的一种。

原文地址:https://www.cnblogs.com/shsgl/p/4289883.html