node package manager
We need your input. Help make JavaScript better: Take the 2017 JavaScript Ecosystem survey »



A bit of sugar for defining JavaScript Objects and their prototypes.

  • A small wrapper around creating JavaScript objects, does not try turn JavaScript into something it's not. (ie classes)
  • Define objects without having to deal with the awkward and verbose syntax found in ES3 & ES5.
  • Painless inheritance, initialization and static methods of the parent class are automatically carried over to the child class.
  • After inheritance, the constructor property on the prototype correctly points to it's constructor, unlike common js inheritance patterns.
  • Provides a simple mechanism for mixins.
  • What gets created is a plain javascript constructor


  //Create a base class with some default methods 
  var Base = defineObject({
    hello: function() {
      console.log("hello world!");
  //Extend from the base class 
  var Animal = Base.extend({
    //a initialization method with side-effects 
    //that luckily will not get called when the Animal object is extended from 
    __init__: function() {
    sound: 'silence',
    weight: 0,
    vocalize: function() {
  //Create a logger method which will be used as a mixin in our example 
  //any object, or constructor can be used as a mixin 
  var Logger = Base.extend({
    __init__: function() {
      this.logHistory = [];
    log : function(msg) {
  //Define the Dog object extending from Animal while mixing in Logger. 
  var Dog = Animal.extend({
    __init__ : function(weight) {
      this.weight = weight;
      //; -- if you need to have access to the Animal initialization method 
    sound : 'woof',
    vocalize : function() {; // Calling 'super' methods remains about the same. 
      this.log("dog is vocalizing");
    drool: function() {
      console.log("I want what you're having.");
      this.log("dog is drooling")
  var dog = new Dog();
  console.log("History", dog.logHistory);
  console.log( dog instanceof Dog ); //true 
  console.log( dog instanceof Animal); //true 
  console.log( dog.constructor === Dog); //true 
  console.log( dog instanceof Logger); //false  

Extending from plain js constructors

You can use defineObject to extend from plain javascript constructors using defineObject.extend

var Plain = function() { = 'bar';
}; = 'foo';
var Ext = defineObject.extend(Plain, {
    fizz: 'fuzz'
}).mixin({'fud' : 'foo'});
var obj = Ext.create();
console.log(obj instanceof Plain);
console.log(,, obj.fizz, obj.fud);


Mixins can take the form of either an object or a constructor.

if an object is used the object properties are merged into the target prototype, if an init method is defined than it will on instantiation.

var O = defineObject()
    __init__ : function() {
      this.msg = "hello world!";
    print : function() {
    __init__ : function() {
      this.value = "hello earth!";
    show : function() {
var o = new O();
o.print() //"hello world!"; //"hello earth!"; 

If a constructor is passed into the mixin method, the init method on the prototype will be used upon instantiation. If an init method does not exists, the constructor method itself will be used.

Static Methods

Static methods can be defined by passing in a second object literal to defineObject or extend;

var O = defineObject({
    instanceProp: 'foo'
    staticProp: 'bar'

Note: defineOjbect will not allow you to redefine existing static properties, an exception will be thrown.

[ES5 ONLY] Properties

calling the 'properties' method on an object definition will simply pass the assigned literal to Object.defineProperties upon the creation of an object.


  var O = defineObject()
      field : {
            value : 'value',
            writable : false
  var o = O.create();
  o.field = 'error'; //error 

Chainable style

Objects can be defined in a more chainable style as well:

  Dog = Animal.extend()

Where 'methods' and 'statics' replace the first two arguments of the extend/defineOjbect function. Both functions can also be used to extend the prototype/static methods of an Object.