Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 22nd, 2012  |  syntax: None  |  size: 1.03 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Oject Oriented Javascript
  2. var Person = Class.extend({
  3.   init: function(isDancing){
  4.     this.dancing = isDancing;
  5.   },
  6.   dance: function(){
  7.     return this.dancing;
  8.   }
  9. });
  10. var Ninja = Person.extend({
  11.   init: function(){
  12.     this._super( false );
  13.   },
  14.   dance: function(){
  15.     // Call the inherited version of dance()
  16.     return this._super();
  17.   },
  18.   swingSword: function(){
  19.     return true;
  20.   }
  21. });
  22.  
  23. var p = new Person(true);
  24. p.dance(); // => true
  25.  
  26. var n = new Ninja();
  27. n.dance(); // => false
  28. n.swingSword(); // => true
  29.  
  30. // Should all be true
  31. p instanceof Person && p instanceof Class &&
  32. n instanceof Ninja && n instanceof Person && n instanceof Class
  33.        
  34. Module("Test", function (m) {
  35.     Class("Point", {
  36.         has: {
  37.             x: {
  38.                 is:   "rw",
  39.                 init: 0
  40.             },
  41.             y: {
  42.                 is:   "rw",
  43.                 init: 0
  44.             }
  45.         },
  46.         methods: {
  47.             clear: function () {
  48.                 this.setX(0);
  49.                 this.setY(0);
  50.             }
  51.         }
  52.     })
  53. })