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 JClass = require"jclass";var Cat = JClass_extend// constructorthiscolor = color;// instance methodconsole.log"An abstract cat cannot meow!";// instance methodreturn thiscolor;;var GrumpyCat = Cat_extend// constructorinit:init_supercallthis "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_extendsJClass; // 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 JClass = require"jclass";var Cat = JClass_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 JClass = require"jclass";var MyClass = JClass_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 JClass = require"jclass";var EventEmitter = require"events"EventEmitter;var Emitter = JClass_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:
_extend(instanceMembers, classMembers): Derives a new class with instanceMembers and classMembers (example).
_extends(JClass): Returns true (false) if
JClassis (is not) a super class.
_superClass: The super class (not available for the base
_subClasses: An array containing all (directly inheriting) sub classes.
_members: A jclass instance holding the class members (example).
JClass has additional attributes that are not propagated to derived classes:
_convert(cls, options): Converts a prototype based class cls into a jclass (example).
_construct(cls, args): Returns an instance of cls, instantiated with args. This is an apply-like usage for the new operator.
All instances have the following attributes:
_class: The class of this instance.
Within instance methods, the super method is always referenced as
_super. You can access them by making your instance method a named function (example).
Marcel R. (riga)