class.js
Simple (2KB minified & gzipped & all features) class system for JavaScript. Designed to work with backbone.js and node.js.
Features
- Core:
- Inheritance
__super__
- reference to super class
- Plugins:
- Mixins
- Interfaces
- Singleton
- Namespaces
- Properties
- getter / setter (Optional:Support for ECMA5 getter / setter)
- visibility
- type check (Class, Object, String, Number, Boolean, Function, Array, Element, Regex, Date, more can be added)
- format value
- validation
- init value,
- nullable
- events (optional, event emitter needed: e.g. Backbone.Events, node.js events.EventEmitter)
- Extensible: Add your own definition keys and types
- Browser & commonJS support
- Well tested
Dependencies
Installation
Browser:
node.js:
npm install classjs
;
Example
var Class = ;var EventEmitter = EventEmitter; var ICompany = { // Check passed arguments if Arrayprototypeslicelength != 1 throw "Wrong length of arguments"; if typeof employe !== "object" throw "Wrong type for argument employe"; }; // Define a new class. Extend from EventEmitter.var Company = Class; // Extend from "Company"var MyCompany = Company; var company = ;company;company;company;
Tests & Custom Builds
class.js is using grunt (https://github.com/cowboy/grunt) to build the modules and to run the tests. If you would like to create a custom build of class.js or develop a feature, please follow these steps:
To install Grunt run the follwing command (npm (http://npmjs.org/) needed):
npm install -g grunt
Test & Build the project, run:
grunt
Automatically build project whenever a lib/*.js file is changed:
grunt watch
Run tests only:
grunt qunit
The tests are located in the test
folder. With the index*.html files you can run the tests manually in any browser you want.
API
Class
- name: The name of the class (optional)
- superClass: Class (optional)
- definition: Object (optional)
Defines a new class.
Class
SomeClass
- name: The name of the class (optional)
- definition: Object (optional) (see Class.extend for more details)
The extend
method is added automatically to every created Class. Extends from the given class.
var MyClass = Class; var SomeClass = MyClass; var obj = ;
Advanced
Sometimes it can be usefull not to use the full feature set of class.js. You can use the methods that are used for the class definitions standalone as well:
ClassClassClassClassClassClassClass
Extending types
Class
For example:
Classtypes"MyType" = { return value instanceof MyType;}
Extending definition
Classdefinition
For example:
Classdefinition;Class { for var key in definition clazzprototypekey = { ; } }
Class define hooks
ClassonBeforeClassDefineClassonAfterClassDefine
For example:
Class { console; }; Class { console;};
Before instantiation hooks
ClassonBeforeInstantiationClassonAfterInstantiation
For example:
Class { console;}; Class { console;};
ECMA5 Getter / Setter
When ECMA5 Mode is activated instead of setProp / getProp methods, ECMA5 getter and setter are generated. This can be used like normal JavaScript properties, but will check for types / validate / format / etc.
ClassECMA5;
For example:
ClassECMA5 = true; var MyClass = Class; var obj = ;objbar = 1 // no setBar(1) neededvar bar = objbar; // no getBar() neededobjfoo = "string" // will throw an exception
Error Types
ClassValidationErrorClassTypeError // extends from validation error
In your error handler you can check for the type:
if error instanceof ClassTypeError console; console; console; console; if error instanceof ClassValidationError console; console; console;
or use it for validation:
var MyClass = Class; var data = foo : true bar : "Some String" ; var obj = ; var errors = ; var { errors = ; for var property in data try obj; catch exc errors; return errorslength === 0; } for var property in data if console; else console;
Version History
- 0.8.0 (2013/11/23, published to npm)
- Named Classes
- Namespace can now have a different context
- 0.7.1 (2012/12/19, published to npm)
- Fixed constructor after inheritance
- 0.7.0 (2012/8/26, published to npm)
- Enhanced
__super__
functionality - node.js example
- Bugfixes
- 0.6.2 (2012/8/5, published to npm)
- Added "Date" type to properties
- Refactoring
- 0.6.1 (2012/7/29, published to npm)
- Improved ECMA5 property support
- Do not set the same value for a property twice
- Removed Class.error handling from property system
- Bugfixes
- 0.6 (2012/7/28, published to npm)
- Splitted up files
- Use grunt to build project
- Interfaces can now have parameter checks
- 0.5.2 (2012/7/27, published to npm)
- Added node tests
- 0.5.1 (2012/7/27, published to npm)
- Node.js fixes
- 0.5 (2012/7/27, published to npm)
- Added ECMA5 getter/setter
- API stabilization / changes
- Removed "notfire" event parameter from property setter
- 0.4 (2012/7/27, not published to npm)
- Improved plugin system
- Added strict mode
- API stabilization / changes
- Removed instanceOf method
- Class.definition is now an array to ensure order of execution
- Before / After Class definition hooks
- Before / After instantiation hooks
- Bugfixes
- More tests
- 0.3 (2012/7/27, not published to npm)
- Plugin system
- QUnit tests
- Namespaces
- Bugfixes
- Validation can be an array of validators
- Simple properties (prop:type) now possible
- Valiator & Formater can now be a member function
- More documentation
- 0.2 (2012/7/26)
- Bugfixes
- API stabilization
- Documentation
- 0.1.1 (2012/7/26)
- Minor Bugfixes
- Docu
- 0.1 (2012/7/26)
- Inital release
Contribute
- If you have a feature request or an issue please report it here: https://github.com/tjbutz/class.js/issues
- If you have developed a plugin for class.js please let me know. I will list the plugin on this page.
- If you use class.js in your project please let me know. I will list the project on this page.