Advertisement
Guest User

Class Inheritance

a guest
Nov 28th, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var
  2.   __bind = function(fn, me){
  3.     return function(){
  4.       return fn.apply(me, arguments);
  5.     };
  6.   },
  7.  
  8.   __hasProp = {}.hasOwnProperty,
  9.  
  10.   __extends = function(child, parent) {
  11.         for (var key in parent) {
  12.             if (__hasProp.call(parent, key))
  13.         child[key] = parent[key];
  14.     }
  15.  
  16.     function ctor() {
  17.       this.constructor = child;
  18.     }
  19.  
  20.     ctor.prototype = parent.prototype;
  21.     child.prototype = new ctor();
  22.     child.__super__ = parent.prototype;
  23.     return child;
  24.   };
  25.  
  26. var Animal = (function() {
  27.  
  28.   function Animal(name) {
  29.     this.name = name;
  30.     this.move = __bind(this.move, this);
  31.  
  32.   }
  33.  
  34.   Animal.prototype.move = function(meters) {
  35.     this.position += meters;
  36.     return console.log(this.name + (" moved " + meters + "m."));
  37.   };
  38.  
  39.   Animal.prototype.position = 0;
  40.  
  41.   return Animal;
  42.  
  43. })();
  44.  
  45. var Snake = (function(_super) {
  46.  
  47.   __extends(Snake, _super);
  48.  
  49.   function Snake() {
  50.     this.slither = __bind(this.slither, this);
  51.     return Snake.__super__.constructor.apply(this, arguments);
  52.   }
  53.  
  54.   Snake.prototype.slither = function() {
  55.     return this.move(3);
  56.   };
  57.  
  58.   return Snake;
  59.  
  60. })(Animal);
  61.  
  62. var Horse = (function(_super) {
  63.  
  64.   __extends(Horse, _super);
  65.  
  66.   function Horse() {
  67.     this.gallop = __bind(this.gallop, this);
  68.     return Horse.__super__.constructor.apply(this, arguments);
  69.   }
  70.  
  71.   Horse.prototype.gallop = function() {
  72.     return this.move(10);
  73.   };
  74.  
  75.   return Horse;
  76.  
  77. })(Animal);
  78.  
  79. var sam = new Snake("Sammy the Python");
  80.  
  81. var tom = new Horse("Tommy the Palomino");
  82.  
  83. sam.move(1);
  84.  
  85. sam.slither();
  86.  
  87. tom.gallop();
  88.  
  89. console.log("" + sam.name + " moved " + sam.position + "m total");
  90.  
  91. console.log("" + tom.name + " moved " + tom.position + "m total");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement