Home
Manage Your Code
Snippet: Extend prototype method (JavaScript)
Title: Extend prototype method Language: JavaScript
Description: One way of extending prototypes in javascript to simulate class inheritance. Views: 219
Author: Ben Ramey Date Added: 11/16/2009
Copy Code  
//
// 'extend' code from Mozilla
// https://developer.mozilla.org/En/Core_JavaScript_1.5_Guide/Inheritance
// Used to set up inheritance between JS types.
//
extend: function(childtype, supertype)
{
  for (var property in supertype.prototype)
  {
    if (typeof childtype.prototype[property] == "undefined")
      childtype.prototype[property] = supertype.prototype[property];
  }

  // so you can call this.__super.prototype.METHOD_NAME.apply(this, args[]);
  // to call overridden superclass methods
  // this.__super.call(this, arg1, arg2, ...) calls the parent constructor
  childtype.prototype.__super = supertype;

  return childtype;
}
Usage
Used to extend one prototype with another to inherit methods and properties from the parent prototype.