# simple-cls

> A Simple Javascript Class System for Node

Latest version **0.0.8** (published 2013-08-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install simple-cls
pnpm add simple-cls
yarn add simple-cls
bun add simple-cls
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.8 |
| Published | 2013-08-01 |
| First published | 2013-06-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.10.0 |
| Dependencies | 1 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Denny C. Dai |
| Maintainers | dennycd |
| Keywords | class, javascript, object, oriented |

## Links

- npm: https://www.npmjs.com/package/simple-cls
- Repository: https://github.com/dennycd/node-cls
- Issues: https://github.com/dennycd/node-cls/issues
- npm.io page: https://npm.io/package/simple-cls

## Dependencies (1)

- [node-uuid](https://npm.io/package/node-uuid.md) 1.4.0

## Recent versions

- 0.0.8 (latest) — 2013-08-01
- 0.0.7 — 2013-07-13
- 0.0.6 — 2013-07-13
- 0.0.5 — 2013-06-19
- 0.0.4 — 2013-06-13
- 0.0.3 — 2013-06-13
- 0.0.2 — 2013-06-12
- 0.0.1 — 2013-06-12

## README

# 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](http://shop.oreilly.com/product/9780596805531.do). 

## Usage

Install the package "simple-cls"
```javascript
npm install simple-cls
```
To define a class, do the following 
```javascript
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: {  
		foo: function() {
			console.log("foo");
		}
	},
	//instance variables
	variables: {
		bar : 1,
		msg : null
	},
	// class level variables or methods
	statics : {
		
	}
});
```

To instantiate an object and start using it  
```javascript
var obj = new MyClass("hello");
obj.foo();
console.log(obj.bar);
```


## Concept

![Concept](https://raw.github.com/dennycd/node-cls/master/doc/node-cls.001.png "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.
```javascript
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
```javascript 
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 
```javascript
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 
```javascript
test.ok(obj.getClass().isKindOfClass('MyClass'));
test.ok(obj.getClass().isKindOfClass('BaseClass'));
test.ok(obj.getClass().isKindOfClass(MyClass.__class));
test.ok(obj.getClass().isKindOfClass(BaseClass.__class));
```

To look at all the methods callable from the obj, including methods defined on the super class
```javascript 
console.log(obj.getClass().getMethods());
```

For details, please look at the unit test examples. 



## Author 
Denny C. Dai <dennycd@me.com> or visit <http://dennycd.me>

## License 
[The MIT License](http://opensource.org/licenses/MIT)

---
_Source: https://npm.io/package/simple-cls · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
