/documentation/js/classes.js

http://github.com/jashkenas/coffee-script · JavaScript · 57 lines · 38 code · 18 blank · 1 comment · 2 complexity · 3ddf901f607c4d71382fad058e948a3a MD5 · raw file

  1. // Generated by CoffeeScript 1.10.0
  2. var Animal, Horse, Snake, sam, tom,
  3. 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; },
  4. hasProp = {}.hasOwnProperty;
  5. Animal = (function() {
  6. function Animal(name) {
  7. this.name = name;
  8. }
  9. Animal.prototype.move = function(meters) {
  10. return alert(this.name + (" moved " + meters + "m."));
  11. };
  12. return Animal;
  13. })();
  14. Snake = (function(superClass) {
  15. extend(Snake, superClass);
  16. function Snake() {
  17. return Snake.__super__.constructor.apply(this, arguments);
  18. }
  19. Snake.prototype.move = function() {
  20. alert("Slithering...");
  21. return Snake.__super__.move.call(this, 5);
  22. };
  23. return Snake;
  24. })(Animal);
  25. Horse = (function(superClass) {
  26. extend(Horse, superClass);
  27. function Horse() {
  28. return Horse.__super__.constructor.apply(this, arguments);
  29. }
  30. Horse.prototype.move = function() {
  31. alert("Galloping...");
  32. return Horse.__super__.move.call(this, 45);
  33. };
  34. return Horse;
  35. })(Animal);
  36. sam = new Snake("Sammy the Python");
  37. tom = new Horse("Tommy the Palomino");
  38. sam.move();
  39. tom.move();