pseudoclass

> An OOP framework for Node.js and the browser

PseudoClass

An OOP framework for Node.js and the browser

PseudoClass provides construct(), destruct(), _super(), and an init() method that runs after construction is complete.

PseudoClass uses JavaScript constructors and prototypal inheritance under the hood. Monkey-patching, instanceof, and instance.constructor all work as expected.

Mixins can be added when a class is declared using the mixins option or after instantiation with the instance.mixin() method.

Stay classy and boilerplate-free with string-based toString declarations and automatic chaining of construct() and destruct().

Make instance properties non-writable, non-enumerable, or employ setters & getters with the properties option, then inherit and override individually.

PseudoClass is completely standalone. All you need to stay classy is Class.js.

As PseudoClass makes use of ECMAScript 5 features, it is only compatible with modern browsers.

  • IE 9+
  • Firefox 4+
  • Chrome 6+
  • Safari 5+
  • Opera 12+
  • Node 0.8+

PseudoClass can be used in a Node, AMD, or browser environment out of the box.

PseudoClass empowers you without getting in your way. See the examples below to see how PseudoClass makes prototypal inheritance painless.

var Parent = Class({
  toString: 'Parent',
  properties: {
    visible: {
      value: true,
      enumerable: true
    }
  },
  constructfunction() {
    console.log('Parent: Constructing');
  },
  destructfunction() {
    console.log('Parent: Destructing');
  },
  doStufffunction() {
    console.log('Parent: Doing stuff');
  }
});

A mixin is a set methods you can plug into any class. Mixins can use _super, just like normal methods.

var stuffDoer = {
  doStufffunction() {
    this._super();
    console.log('Mixin: Doing stuff');
  }
};

Mixins added at declaration time become part of the prototype.

var Child = Parent.extend({
  toString: 'Child',
  mixins: [stuffDoer],
  properties: {
    visible: {
      value: false // Only override the value 
    }
  },
  constructfunction() {
    console.log(this+': Constructing');
  },
  destructfunction() {
    console.log(this+': Destructing');
  },
  doStufffunction() {
    this._super();
    console.log(this+': Doing stuff');
  }
});
var child = new Child();
/* Output:
  Parent: Constructing
  Child: Constructing
*/
child.doStuff();
/* Output:
  Parent: Doing stuff
  Child: Doing stuff
  Mixin: Doing stuff
*/

Mixins added after instantiation become part of the instance.

child.mixin({
  doMoreStufffunction() {
    console.log(this+': Doing more stuff')
  }
});
 
child.doMoreStuff();
/* Output:
  Child: Doing more stuff
*/
console.log(child.instanceOf(Child)); // true 
console.log(child.instanceOf(Parent)); // true 
console.log(child.constructor === Child) // true 
console.log(child+''); // 'Child' 
child.destruct();
/* Output:
  Child: Destructing
  Parent: Destructing
*/

PseudoClass is always accessible as PseudoClass. If you're using another library that defines Class, you can still use PseudoClass by referencing PseudoClass instead.

PseudoClass is licensed under the permissive BSD license.