JavaScript基础(9) -- JavaScript面向对象的方法实现继承:call方法

  1. // 动物类 animal  

  2. function animal(bSex){  

  3.     this.sex = bSex  

  4.     this.getSex = function(){  

  5.         return this.sex  

  6.     }  

  7. }  

  8. // 类静态变量 (如果你不修改它的话~~)  

  9. animal.SEX_G = new Object();    // 雌性  

  10. animal.SEX_B = new Object();    // 雄性  

  11. // 动物子类 鸟  

  12. function bird(bSex){  

  13.     animal.call(this, bSex);  

  14.     this.fly = function(iSpeed){  

  15.         alert("飞行时速高达 " + iSpeed);  

  16.     }  

  17. }  

  18. // 动物子类 鱼  

  19. function fish(bSex){  

  20.     animal.call(this, bSex);  

  21.     this.swim = function(iSpeed){  

  22.         alert("游动时速高达 " + iSpeed)  

  23.     }  

  24. }  

  25. // 鱼 鸟 杂交品种。。。  

  26. function crossBF(bSex){  

  27.     bird.call(this, bSex);  

  28.     fish.call(this, bSex);  

  29. }  

  30. var oPet = new crossBF(animal.SEX_G);    // 雌性 鱼鸟  

  31.     alert(oPet.getSex() == animal.SEX_G ? "雌性" : "雄性");  

  32.     oPet.fly(124)  

  33.     oPet.swim(254)  

posted @ 2016-12-08 20:16  稼轩  阅读(112)  评论(0编辑  收藏  举报