Advertisement
JordanScripter1337

ObjectOriented

Sep 18th, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Animal, Horse, Snake, sam, tom,
  2.   extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  3.   hasProp = {}.hasOwnProperty;
  4. // Declaring the superclass
  5. Animal = (function() {
  6.   function Animal(name) {
  7.     this.name = name;
  8.   }
  9.  
  10.   Animal.prototype.move = function(meters) {
  11.     return alert(this.name + (" moved " + meters + " meters."));
  12.   };
  13.  
  14.   return Animal;
  15.  
  16. })();
  17.  
  18. Snake = (function(superClass) {
  19.   extend(Snake, superClass);
  20.  
  21.   function Snake() {
  22.     return Snake.__super__.constructor.apply(this, arguments);
  23.   }
  24.  
  25.   Snake.prototype.move = function() {
  26.     alert("Slithering...");
  27.     return Snake.__super__.move.call(this, 5);
  28.   };
  29.  
  30.   return Snake;
  31.  
  32. })(Animal);
  33.  
  34. Horse = (function(superClass) {
  35.   extend(Horse, superClass);
  36.  
  37.   function Horse() {
  38.     return Horse.__super__.constructor.apply(this, arguments);
  39.   }
  40.  
  41.   Horse.prototype.move = function() {
  42.     alert("Galloping...");
  43.     return Horse.__super__.move.call(this, 45);
  44.   };
  45.  
  46.   return Horse;
  47.  
  48. })(Animal);
  49.  
  50. sam = new Snake("Sammy the Snake");
  51.  
  52. tom = new Horse("Tommy the Horse");
  53.  
  54. sam.move();
  55.  
  56. tom.move();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement