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.
Usage
Install the package "simple-cls"
npm install simple-cls
To define a class, do the following
var cls = var MyClass = cls;
To instantiate an object and start using it
var obj = "hello";obj;console;
Concept
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;)
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
The Class object contains runtime information about the obj class and can be obtained via __class
on the constructor or .getClass()
methods
assert
To check if a js object is an instance of a class, or an instance of a subclass of some super classes
test;test;test;test;
To look at all the methods callable from the obj, including methods defined on the super class
console;
For details, please look at the unit test examples.
Author
Denny C. Dai dennycd@me.com or visit http://dennycd.me