node-class
Introduction
This is a classical object oriented but with the particular philosophy of Javascript. That means the usage of prototypes and defineProperty.
What you can expect:
- extends multiple classes (be careful with the order, it's important).
- implements an interface (so have interfaces).
- final (wip).
- hidden, alias to enumerable:false.
- const, alias to writable:false.
- some helper methods like:.
- serialize
- unserialze
- inspect for proper inspecting in node, avoid "[Getter/Setter]"
- clone (wip).
What you will not find, ever:
- protected or private, both require eval or the use of arguments.callee, that will hurt performance, and I'm very serious about top performance.
- instanceof X, the existence of interfaces and multiple inheritance disallow it use.
TODO list
- final methods.
- examples for: accesors (getter/setters).
- examples for: static.
- examples for: Iterable.
- fix Eventize attached the same event name to a different classes.
- examples for: debug mode ON/OFF usage and consecuences in performance.
Class / Interfaces
Everything is best explained with a good test.
// note: "check_if" is a node-tap test// this is part of test/test-class.js var $ = // require("node-class") __class = $class Character Player goku vegetta; // Create a Character classCharacter = ; // now define an abstract method// you could use "abstract method_name": function() {} insteadCharacter; // if you try to create an instance, throws!check_if; // now we create the NPC class Player = ; goku = name: "goku" // this auto set your properties! hp: 10 // but if you are evil, we don't let you! // remember: it's not merge! it's auto-set! donotexist: true; check_if;check_if; vegetta = name: "vegetta" hp: 10; // goku attack vegettagoku;check_if;check_if; // inheritancecheck_if;check_if; // if you prefer the you can send the string.check_if;
Another example...
// properties usage, the node-class "way of thinking"var Storage = ; // let's look instances behaviorvar st01 = boxes: peaches: 50 unboxed: cherries: 120 ; // first we are going to check the hidden property is workingcheck_if; // can we modify it ?check_if; // now the rest of properties!check_if;check_if; check_if; check_if; // a new example, and this is not a bug :)var st02 = boxes: oranges: messing: "more" ; check_if;// this is the expected behavior, in fact is a performance trade of.// node-class keep a recursive typeof of the property and expect you to send// compatible data for cloning. Otherwise, use null// classes are not cloned. // typeof operator extends the functionality given by "object-enhancements" module.check_if;check_if;
Configurator properties.
A configurator is a "property chain" that produce in the end an object. Better with the following example/test
var db = ; db; t; t;t;
Some configuration require more than booleans... in that case...
db; t;
Note, a configurator is static and it'll not be extended. It's supposed to be something like const variable in many languages with more power.
Events (Event emitter)
Events is a flexible and powerful event emitter.
Extending Events & Properties integration.
var Emitter = test_counter = 0 em; { ++test_counter;}; { --test_counter; console;}; em = onCountUp: increment onCountDown: decrement onGoDown: decrement; // note: CountUp was transformed to count-up// if you prefer another notation, override Event.$transform with your own// there are a few already defined like: Event.$transformIntact or Event.$transform_snake_case check_if;check_if;check_if; // fire the event! you have many alias to avoid collisions :)em;em;em; check_if; // now decrementem;check_if; // you can fire with asterisk will fire, go-down and count-downem;check_if; // you can listen once to an eventcheck_if; em; //or X timesem;em;check_if;em;check_if; em;check_if; em;check_if; em;check_if; check_if; em;check_if;
Dependencies
Developement
tap (tests)
ass (code-coverage)
Performance
In order to achieve a good performance, no overhead, node-class do things you should know about.
- You can call instances constructor (initialize method) outside (it's not a flaw in design avoid an apply call)
- You can access sensible information that can destroy your own class, like YourClass.methods, YourClass.properties, etc... print YourClass in console to see the metadata node-class use to create inheritance.
- If you use more than 4 arguments in constructor or a overridden function, apply will be used.
- Everything has debug enabled by default.
Install
With npm do:
npm install node-class
test (travis-ci ready!)
npm test
// or
cd /test
node test-class.js
license
MIT.