Classing
Easy, flexible classes for JavaScript, works in node and all modern browser (> IE8).
Why another class library?
Because none of the other class libs have a good way to associate meta data with properties, and have that meta data easily available in child classes. This is useful for e.g. defining property labels, types, validation rules etc.
Classing presents an API similar to the native Object.defineProperty()
and Object.defineProperties()
methods.
var Class = ; var User = ; User; User; var user = name: 'charles' email: 'foo@example.com'; console; // "Name: charles" console;
The main difference between this and the native methods is that the full descriptor declarations are preserved. The native methods discard these extra keys (anything other than enumerable
, configurable
, writable
, value
, get
and set
), making them unsuitable for storing metadata. Classing corrects this and ensures that the descriptors are accessible within child classes.
Installation
Via npm:
npm install --save classing
or bower:
bower install --save classing
Usage
Simple classes
var Class = ; var Person = ; var person = ; personname === 'No Name'; // => true var person = name: 'Bob' dateOfBirth: ;
Default values
var Collection = ; var list = ; listlength === 0; // true list; listlength === 3; // true
Inheritance
var Vehicle = ; var RoadVehicle = Vehicle; var Car = RoadVehicle; var mini = name: 'mini' capacity: 28; console;
Mixins
var Truck = RoadVehicle; Truck; var truck = ;console;
Auto Binding
var MyConsole = Class; var myconsole = alert = myconsolealert log = myconsolelog; myconsolealertCalledCountshould; ;; myconsolealertCalledCountshould; ;
Running the tests
First, npm install
, then npm test
. Code coverage generated with npm run coverage
.
License
MIT, see LICENSE.md.