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

Classic Inheritance

By: a guest on Jul 13th, 2012  |  syntax: JavaScript  |  size: 0.65 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. // Parent class, inherits from "Object"
  2. function MyClass() {
  3.    var somethingPrivate;
  4.    var somethingElse;
  5.    // Constructor stuff
  6. }
  7. // Only methods on the prototype can be inherited
  8. MyClass.prototype.someMethod = function() { /* blah blah */ }
  9. MyClass.prototype.someOtherMethod = function() { /* blah blah */ }
  10.  
  11. // You use "new" to create objects from the prototype
  12. var obj = new MyClass();
  13.  
  14. // Create a basic child class
  15. function ChildClass() {  /* Constructor stuff */ }
  16.  
  17. // Do the inheritance
  18. ChildClass.prototype = new MyClass();
  19. // Override stuff or add child-specific stuff
  20. ChildClass.prototype.overridenMethod = function() { /* blah */ }