jclass started as a port of John Resig's lightweight OO inheritance model. However, this implementation is faster as it avoids threefold method wrapping (see here). In addition, it provides class members, property descriptors, conversion of prototype-based classes and various conveniences.
jclass has no dependencies and works in most import environments: RequireJS (AMD), CommonJS, NodeJs and web browsers.
jclass is hosted at npmjs. Install it via:
npm install jclass
All examples below use NodeJs but the jclass related code would also work in other environments.
var Class = require"jclass";var Cat = Class_extend// constructorthiscolor = color;// instance methodconsole.log"An abstract cat cannot meow!";// instance methodreturn thiscolor;;var GrumpyCat = Cat_extend// constructorinit:init_supercallthis "greyish";// if you don't want to use a named function you can also use// this.init._super.call(this, "greyish");// instance methodconsole.log"Nah, not in the mood.";;var cat = "black";catmeow; // "An abstract cat cannot meow!"var grumpy = ;grumpymeow; // "Nah, not in the mood."grumpygetColor; // "greyish", same as grumpy.color// instanceof works as expectedconsole.loggrumpy instanceof GrumpyCat; // trueconsole.loggrumpy instanceof Cat; // trueconsole.logGrumpyCat_extendsCat; // true, same as GrumpyCat._superClass == Catconsole.logGrumpyCat_extendsClass; // true
Class members are accessible via the
_members property which is itself a jclass instance. To add class members,
add a second paramter to
var Class = require"jclass";var Cat = Class_extend// instance members// constructorthiscolor = color;// instance methodconsole.log"An abstract cat cannot meow!";// class membersfamily: "Felidae"console.logthisfamily;// same as// console.log(Cat._members.family);;Cat_membersgetFamily; // "Felidae", same as Cat._members.family
Please note that
this within class methods references the
_members instance itself.
All instance and class members given to
_extend can also be applied as property descriptors that are passed to
Object.defineProperty. All you need to do is define members as objects and add a property
descriptor: true. Both, accessor-type and data-type descriptors are supported.
var Class = require"jclass";var MyClass = Class_extendsomeKey:descriptor: truereturn "some value";;var myInstance = ;console.logmyInstancesomeKey; // "some value"
You can convert prototype-base classes into jclasses. This approach supports constructor arguments.
// example using nodejsvar Class = require"jclass";var EventEmitter = require"events"EventEmitter;var Emitter = Class_convertEventEmitter;var emitter = ;emitteron"topic" ;emitteremit"topic" ;;
The instance of the (original) prototyped class is stored as
_origin in each jclass instance.
Classes have the following attributes:
function(instanceMembers, classMembers)): Derives a new class with
Classis (is not) a super class.
Class): The super class (not available for the base
array): An array containing all (directly inheriting) sub classes.
Class instance): A jclass instance that handles the class members (example).
Class has additional attributes that are not propagated to derived classes:
Function(cls, options)): Converts a prototype based class
clsinto a jclass (example).
Function(cls, args)): Returns an instance of
cls, instantiated with
args. This is an
apply-like usage for
All instances have the following attributes:
Class): The class of this instance
Within instance methods, the super method is always referenced as
If the option
true, each instance can directly access class members within instance methods. You can use the
_class attribute outside of instance methods or if
- Source hosted at GitHub
- npm module hosted at npmjs
- Report issues, questions, feature requests on GitHub Issues
Marcel R. (riga)