Simple Inheritance on Javascript Objects

I’ve been playing a lot with javascript lately and needed to extend an object, retaining all the existing methods but also adding more.

This simple script allows exactly that:

Object.prototype.implement = Function.prototype.implement = function(props) {
  for (var key in props) {
    this[key] = props[key];
  return true;

Object.prototype.extend = Function.prototype.extend = function(props) {
  var obj = new this.constructor();
  obj.prototype = this.prototype;
  if (props) {

  return obj;

It should be used similar to how you would extend a class in PHP i guess:

var Class = function() {

Class.prototype.test = function() {

var Class2 = Class.extend();

Class2.prototype.test2 = function() {

var obj = new Class();

var obj2 = new Class2();

// alerts test
// throws an exception
// alerts test
// alerts test2

I know there are many frameworks that probably already do this better, but if you just need a simple object extension method, this should work.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.