PseudoClass

An object oriented programming framework for Node.js and the browser


Sweet syntactic sugar for prototypal inheritance.

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

All the same under the hood.

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

Not afraid to mix it up.

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

Crushes boilerplate with a classy touch.

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

Define and override properties effortlessly.

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

See in action Download

Getting started is easy.

1. Define a class:

Pass an object with class methods to Class() to create a new class.

Include an object of property descriptors as the properties option.

var Parent = Class({ toString: 'Parent', properties: { visible: { value: true, enumerable: true } }, construct: function() { console.log('Parent: Constructing'); }, destruct: function() { console.log('Parent: Destructing'); }, doStuff: function() { console.log('Parent: Doing stuff'); } });

2. Define a mixin

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

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

3. Inherit from Parent, override, properties, and mix in a mixin

Mixins can be added to declaration time with the mixins option.

Child classes will inherit properties from their parents and can override individual properties of their descriptors. In this case, the visible property will still be enumerable, but its default value will be overridden to false

var Child = Parent.extend({ toString: 'Child', mixins: [stuffDoer], properties: { visible: { value: false // Only override the value } }, construct: function() { console.log(this+': Constructing'); }, destruct: function() { console.log(this+': Destructing'); }, doStuff: function() { this._super(); console.log(this+': Doing stuff'); } });

4. Create an instance

Fire it up! construct() is automatically chained, and the with the parent-most construct() method called first.

var instance = new Child(); /* Output: Parent: Constructing Child: Constructing */

5. Call a method

Watch call overridden methods for you when you use this._super().

instance.doStuff(); /* Output: Parent: Doing stuff Child: Doing stuff Mixin: Doing stuff */

5. Add a mixin to the instance

Mixins can be added after the class is instantiated.

instance.mixin({ doMoreStuff: function() { console.log(this+': Doing more stuff') } }); instance.doMoreStuff(); /* Output: Child: Doing more stuff */

6. Check yo' self

instanceof and instance.constructor work as expected.

console.log(instance.instanceOf(Child)); // true console.log(instance.instanceOf(Parent)); // true console.log(instance.constructor === Child) // true console.log(instance+''); // 'Child'

7. Wreck yo' self

If you've defined a destruct() method, call it when you're done with an instance. It's automatically chained, with the child-most destruct() method called first.

instance.destruct(); /* Output: Child: Destructing Parent: Destructing */
Maintained by lazd