IT-Academy

JS Polymorfizmus

Apr 5th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. function Zviera() {
  8.     this.meno = "Zviera";
  9.     this.toString = function () {
  10.         return "Moje meno je: " + this.meno;
  11.     };
  12. }
  13.  
  14. function PsoviteSelmy() {
  15.     this.meno = "Psovite selmy";
  16. }
  17.  
  18. function Vlk() {
  19.     this.meno = "Vlk";
  20. }
  21.  
  22. PsoviteSelmy.prototype = new Zviera();
  23. Vlk.prototype = new PsoviteSelmy();
  24.  
  25. PsoviteSelmy.prototype.constructor = PsoviteSelmy;
  26. Vlk.prototype.constructor = Vlk;
  27.  
  28. var arktickyVlk = new Vlk();
  29. document.write(arktickyVlk.toString() + "<br />");
  30. document.write("Je arkticky vlk vlkom: " + (arktickyVlk instanceof Zviera) + "<br />");
  31.  
  32. Zviera.prototype.zvuk = "Grrrrr";
  33. Zviera.prototype.getZvuk = function () {
  34.     return "Je to: " + this.meno + " a robi: " + this.zvuk;
  35. };
  36.  
  37. PsoviteSelmy.prototype.zvuk = "Haf";
  38. Vlk.prototype.zvuk = "Grrr haf";
  39.  
  40. document.write(arktickyVlk.getZvuk() + "<br />");
Advertisement
Add Comment
Please, Sign In to add comment