amigojapan

javascript ineritance

Feb 10th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. output:
  3. flappie flappie
  4. egg layed
  5. swimmy swimmy
  6. walkie walkie
  7. eww, worms
  8. nom, nom
  9. swimmy swimmy
  10. swimmy swimmy
  11. nom, nom
  12. undefined
  13. */
  14. function Bird(){
  15.   this.layegg = function (){
  16.     console.log("egg layed");
  17.   }
  18.   this.walk = function(){
  19.     console.log("walkie walkie");
  20.   }
  21. }
  22.  
  23. Bird.prototype.eatWorms = function(){
  24.   console.log("eww, worms");
  25. }
  26.  
  27. function Penguin(){
  28.   Bird.call(this);
  29.   this.swim = function(){
  30.     console.log("swimmy swimmy")
  31.   }
  32. }
  33.  
  34. Penguin.prototype = Object.create(Bird.prototype);
  35.  
  36. function Duck(){
  37.   Bird.call(this);
  38.   this.fly = function(){
  39.   console.log("flappie flappie")
  40.   }
  41. }
  42.  
  43. duck1 = new Duck();
  44. duck1.fly();
  45. duck1.layegg();
  46.  
  47. penguin1 = new Penguin();
  48. penguin1.swim();
  49. penguin1.walk();
  50. penguin1.eatWorms();
  51. Bird.prototype.eatFish = function(){
  52.   console.log("nom, nom");
  53. }
  54. penguin1.eatFish();
  55. penguin1.swim();
  56.  
  57. penguin2 = new Penguin();
  58. penguin2.swim();
  59.  
  60.  
  61.  
  62.  
  63.  
  64. penguin2.eatFish();
Add Comment
Please, Sign In to add comment