new Class(definition, traits...);
- definition - String, Function, Object.
- traits... - String, Function, Object.
Constructs JavaScript classes that provide easy type introspection and remove the need for boilerplate code. Also has inheritance and depedency injection built in. Aims to be a lightweight, and simple implementation that can be easily monkey patched if needed.
Returns a Function.
"use strict"; { return thisinitialize; }
Class.construct(constructor, traits...);
- constructor - String, Function, Object.
- traits... - String, Function, Object.
Returns a Function.
Class
Class.helpers;
A home for helper methods that do not need to be class or instance methods.
Classhelpers = {};
Class.helpers.get_traits(constructor);
Classhelpers
Class.helpers.get_class_constructor(object);
Classhelpers { return objectclass_constructor || objectconstructor && objectconstructorclass_constructor ; }; Classclass_constructor =Classprototypeclass_constructor = Class;
Class.helpers.get_object_class(class_constructor);
Classhelpers { class_constructor = Classhelpers; if !class_constructornamespace Object; return class_constructornamespace; };
Class.helpers.get_dependencies(object);
Classhelpers { if !objectdependencies Object; return objectdependencies; };
Class.helpers.get_super_class(constructor);
Classhelpers
Class.helpers.eval_function(name, args, body);
Classhelpers { return 'indirect' eval'(function ' + name + '(' + args + '){' + body + '})'; };
Class.helpers.namespace_upsert_constructor(class_constructor, constructor);
Classhelpers
Class.helpers.new_error(error_message);
Classhelpers
Class.helpers.parse_class_constructor_source(class_constructor);
Classhelpers { var source; if !class_constructorargument_names source = class_constructor; class_constructorargument_names = source; if !class_constructorfunction_body source = source || class_constructor; class_constructorfunction_body = source };
Class.helpers.spawn_constructor(class_constructor, constructor_name);
Classhelpers { Classhelpers; return Classhelpers; };
Class.helpers.spawn_prototype_constructor(class_constructor, constructor_prototype);
Classhelpers { var
Class.instance;
New classes start with Class.instance as a trait. This provides default instance methods for new classes.
Classinstance = {};
Class.instance.initialize();
Classinstance { return this; };
Class.instance.inject(overrides);
Classinstance { if Array overrides; var namespace = Classhelper; if thisdependencies && thisdependenciesforEach thisdependencies; return this; };
Class.prototype;
Properties are copied from Class.prototype and given to new classes. This is necessary because it is currently not possible to use Object.create() on a Function.
Class.prototype.include(derivatices...);
Classinclude =Classprototype { var new_derivatives = slice; new_derivatives; return this; };
Class.prototype.create(object, constructor);
Classcreate =Classprototype
Class.prototype.decorate(trait);
Classdecorate =Classprototype { var traits = Classhelpers namespace = Classhelpers; if !trait throw Classhelpers; if traits !== -1 throw Classhelpers; if trait && typeof traitforEeach === 'function' trait; return this; if typeof trait === 'string' && namespacetrait trait = namespacetrait; if typeof trait === 'function' if traitname && namespacetraitname === trait traitstraitname = trait; else if typeof trait !== 'object' throw Classhelpers; traits; return this; };
Class.prototype.define(name, value, context);
Classdefine =Classprototype { if name === 'constructor' return; context = context || thisprototype || this; if name === 'dependencies' var dependencies = Classhelpers; if value && valueforEach value; else ; return contextname = value; { if typeof dependency === 'string' contextdependencies; } };
Class.prototype.extend(constructor);
Classextend =Classprototype
Class.prototype.composed(object);
- object - Object.
Checks if an object is an instance of the this class, or if this class is a trait of the object and therefore helped in its composition.
Returns a Boolean.
Classcomposed =Classprototype { if object === this return true; if object instanceof this return true; if object return true; var object_constructor = objectconstructor; if !object_constructor return false; if object_constructor === this || object_constructor === Object return true; var object_traits = Classhelpers; if object_traitsthisname === this return true; if object_traits !== -1 return true; if objectsuper_class && objectsuper_class return this; var super_class = Classhelpers; if super_class && super_classderives return super_class; return false; };
Class.prototype.inherits(super_class);
Classinherits =Classprototype { var current_super_class = Classhelpers; if current_super_class throw Classhelpers; var derivatives = Classhelpers namespace = Classhelpers; if typeof super_class === 'string' super_class = namespacesuper_class; if typeof super_class !== 'function' || !super_classname throw Classhelpers; if super_classcreate thisprototype = super_class; else thisprototype = Object; Object; Object; super_class; this; return this; };
Class.prototype.initialize(definition, traits...);
- definition - String, Function, Object.
- traits... - String, Function, Object.
Returns a Function.
Classinitialize =Classprototype { return Classconstruct; };
Class.prototype.install(object, trait);
- object - any Object.
- trait - String | Function | Object.
Gives an object a trait.
Returns this Function - class constructor.
Classinstall =Classprototype { object = object || Object; var traits = Classhelpers namespace = Classhelpers; if typeof trait === 'string' trait = namespacetrait; if typeof trait === 'function' if typeof traitcreate === 'function' trait; else trait; if typeof trait === 'object' && trait if typeof traitconstructor === 'object' && traitconstructor Object; Object; return this; };
Class.prototype.toSouce();
Returns a String - the raw source of the constructor function.
if !ClassprototypetoSource ClasstoSource = ClassprototypetoSource = FunctionprototypetoSource || FunctionprototypetoString;
Class.prototype.toString();
Returns a String - the name of the class.
ClasstoString =Classprototype { return thisname; };
Class.register(constructor);
- constructor - String, Function, Object.
Creates a new constructor function with a name and stores it in a namespace.
Returns a Function.
Class
Class.version;
Exposes the semantic version number.
Classversion = '0.0.1';