A Simple Javascript Class System for Node

Simple Javascript Class System for Node.js

node-cls is a simple class system that adds a number of object-oriented features to the basic javascript language, including

  • convinient class definition with inheritance

  • abstract class a.k.a. interface support

  • runtime introspection with dynamic type checking

The design goal is to provide a clean and easily understandable class system while minimizing overhead and avoid poluting the javascript built-in objects. It is inspired and built upon the defineClass from JavaScript The Definitive Guide.

Install the package "simple-cls"

npm install simple-cls

To define a class, do the following

var cls = require('simple-cls')
var MyClass = cls.defineClass({
    //optional class name  
    name : "MyClass",
    //optional super class, default is Object 
    extend : BaseClass,
    //constructor code 
    construct : function(msg){
        this.msg = msg;
    //instance methods 
    methods: {  
        foofunction() {
    //instance variables 
    variables: {
        bar : 1,
        msg : null
    // class level variables or methods 
    statics : {

To instantiate an object and start using it

var obj = new MyClass("hello");;

in node-cls, the class system is built upon a chain of triple objects, namely the Prototype, Constructor and Class. As the diagram shows, instantiating a new js object will establish a relationship from the bottom up, starting at the object instance. Using javascript's built-in function we can obtain the prototype of that object.

assert.ok(Object.getPrototypeOf(obj) === MyClass.prototype);)

The prototype object itself is an object instance of the superclass of MyClass. If no superclass is given, it is defaulted to Object. MyClass is an alias of the Constructor function for obj

assert.ok(obj.constructor === MyClass)

The Class object contains runtime information about the obj class and can be obtained via __class on the constructor or .getClass() methods

assert.ok(obj.constructor.__class === obj.getClass())

To check if a js object is an instance of a class, or an instance of a subclass of some super classes


To look at all the methods callable from the obj, including methods defined on the super class


For details, please look at the unit test examples.

Denny C. Dai or visit

The MIT License