Fork me on GitHub!

Classify.js

Classify.js is a library that allows for cross browser and cross platform Object Oriented Javascript class definitions using classical inheritance patterns and namespaces behind the prototype syntax of javascript in an easy to use interface function.

Classify.js was created to simplify the creation of reusable objects in a manner much like the traditional C based languages. It is a wrapper to the Javascript prototype syntax of class object creation, instead classes are defined in object notation much similar to single instance javascript objects.

Classify.js also provides utilities methods that are useful to javascript classes, such as auto binding methods without adding Function.bind, observable properties with getters, setters, and event listeners, and namespacing that can be easily accessed and externalized.

Source

This project is hosted on GitHub. You can report bugs and suggest new features on the issues page.

A complete Test Suite is available in the repository by running ./make.js unit.

Downloads (Right-click, and use "Save As")

Classify.js is distributed under the MIT license.

Development Version (v0.10.2) 11.91 kb, Uncompressed and Fully Commented
Production Version (v0.10.2) 3.93 kb, Minified and Gzipped

Documentation

Classify

Classify

Classify()
The Main interface function that returns namespaces and creates objects.
Returns Classify.Class
// Different ways to call Classify

// Calling Classify with a string returns a namespace
// @see Classify.getNamespace
Classify("Vehicles") === Classify.getNamespace("Vehicles");

// Calling Classify with a string ending in a "/" returns a namespace
// @see Classify.getNamespace
Classify("Vehicles") === Classify.getNamespace("Vehicles");

// Calling Classify with a string separated by "/" returns a defined
// class within a namespace
// @see Classify.Namespace.get
Classify("Vehicles/Ship") === Classify.getNamespace("Vehicles").get("Ship");

// Calling Classify with 2 string parameters returns a defined class within
// a namespace
// @see Classify.Namespace.get
Classify("Vehicles", "Ship") === Classify.getNamespace("Vehicles").get("Ship");

// Calling Classify with first parameter as a string, and second parameter as
// an object, creates a class within the namespace
var Ship = Classify("Vehicles/Ship", {
    init : function() {
    }
});
// Calling Classify with first parameter as a string, second parameter as a string, and
// third parameter as an object, creates a class within the namespace
var Ship = Classify("Vehicles", "Ship", {
    init : function() {
    }
});
// The above 2 statements are the same as the following statement to create classes
// @see Classify.Namespace.create
var Ship = Classify.getNamespace("Vehicles").create("Ship", {
    init : function() {
    }
});

// To inherit from other classes, the first parameter is the class name, second parameter
// is the parent (can be from other namespaces with "Namespace/Class"), third parameter
// is the descriptor
var Battleship = Classify("Vehicles/Battleship", "Ship", {
    init : function() {
    }
});
// The same can be achieved with the following signature
var Battleship = Classify("Vehicles", "Battleship", "Ship", {
    init : function() {
    }
});
// The above 2 statements are the same as the following statement to create classes
// @see Classify.Namespace.create
var Battleship = Classify.getNamespace("Vehicles").create("Battleship", "Ship", {
    init : function() {
    }
});

// Calling Classify with objects with create classes with the object descriptor.
// Using the Classify method in this manner is analogous to calling Classify.create()
// Please see Classify.create for documentation
// @see Classify.create
var Energy = Classify({
    init : function() {
    }
});
// The above statement is the same as
var Energy = Classify.create({
    init : function() {
    }
});

Classify.version

Classify.version
The version number of this file.
String The version number of this file

Classify.Classify

Classify.Classify
Circular reference to itself.
Function Circular reference to itself

Classify.create

Classify.create([parent][, implement], definition)
Creates a new Classify class.
Arguments
  1. 1. [parent] {Classify.Class} Optional first parameter defines what object to inherit from
  2. 2. [implement] {Object[]} Optional second parameter defines where to implement traits from
  3. 3. definition {Object} The description of the class to be created
Returns Classify.Class

Classify.getNamespace

Classify.getNamespace(namespace)
Retrieves a namespace and creates if it it doesn't already exist.
Arguments
  1. 1. namespace {String} Dot separated namespace string
Returns Classify.Namespace

Classify.destroyNamespace

Classify.destroyNamespace(namespace)
Destroy an existing namespace.
Arguments
  1. 1. namespace {String} Dot separated namespace string

Classify.testNamespace

Classify.testNamespace(namespace)
Retrieves the first namespace that matches the namespace chain "Ns1.ns2.ns3.ns4".
Arguments
  1. 1. namespace {String} Dot separated namespace string
Returns Classify.Namespace

Classify.getGlobalNamespace

Classify.getGlobalNamespace()
Retieves the globally named namespace.
Returns Classify.Namespace

Classify.global

Classify.global
The globally named namespace.
Classify.Namespace The globally named namespace

Classify.addMutator

Classify.addMutator(name, mutator[, mutator.onCreate][, mutator.onPropAdd][, mutator.onPropRemove][, mutator.onInit])
Adds a global class mutator that modifies the defined classes at different points with hooks.
Arguments
  1. 1. name {String} The name of the mutator reference to add
  2. 2. mutator {Object} The mutator definition with optional hooks
  3. 3. [mutator.onCreate] {Function} The hook to be called when a class is defined
  4. 4. [mutator.onPropAdd] {Function} The hook to be called when a property with the __name_ prefix is added
  5. 5. [mutator.onPropRemove] {Function} The hook to be called when a property with the __name_ prefix is removed
  6. 6. [mutator.onInit] {Function} The hook to be called during each object's initialization

Classify.removeMutator

Classify.removeMutator(name)
Removes a global class mutator that modifies the defined classes at different points.
Arguments
  1. 1. name {String} The name of the mutator to be removed

Classify.extend

Classify.extend(base, args)
Utility function to provide functionality to quickly add properties to objects.
Arguments
  1. 1. base {Object} The base object to copy properties into
  2. 2. args {Object[]} Set of objects to copy properties from
Returns Object

Classify.provide

Classify.provide(namespace, base)
Utility function to provide functionality to allow for name provisioning.
Arguments
  1. 1. namespace {String} The dot separated namespace tree to create
  2. 2. base {Object} The object to create the namespace tree within
Returns Object

Classify.noConflict

Classify.noConflict()
Utility function for web to avoid namespace issues with other libraries.
Returns Classify
(function(Classify) {
    // here you can use the Classify object and remove the global reference to it
    // this function is only available on browser environments
})(Classify.noConflict());

Classify.Class

Classify.Class

Classify.Class()
Placeholder for class descriptors created with the create method.
Constructor method

Classify.Class.superclass

Classify.Class.superclass
Reference to the parent that this object extends from.
Classify.Class Reference to the parent that this object extends from

Classify.Class.subclass

Classify.Class.subclass
Array containing a reference to all the children that inherit from this object.
Array Array containing a reference to all the children that inherit from this object

Classify.Class.implement

Classify.Class.implement
Array containing all the objects and classes that this object implements methods and properties from.
Array Array containing all the objects and classes that this object implements methods and properties from

Classify.Class.bindings

Classify.Class.bindings
Array containing the list of all the bound properties that is wrapped during object initialization.
Array Array containing the list of all the bound properties that is wrapped during object initialization

Classify.Class.observables

Classify.Class.observables
Hashtable containing the definitions of all the observable properties that is implemented by this object.
Object Hashtable containing the definitions of all the observable properties that is implemented by this object

Classify.Class.__isclass_

Classify.Class.__isclass_
Flag to determine if this object is created by Classify.create.
Boolean Flag to determine if this object is created by Classify.create

Classify.Class.invoke

Classify.Class.invoke()
Default invocation function when the defined class is called without the "new" keyword. The default behavior is to return a new instance of itself.
Returns Classify.Class

Classify.Class.applicate

Classify.Class.applicate(args)
Create a new instance of the class using arguments passed in as an array.
Arguments
  1. 1. args {Array} Array of arguments to construct the object with
Returns Classify.Class

Classify.Class.extend

Classify.Class.extend([implement], definition)
Creates a new class that is a child of the current class.
Arguments
  1. 1. [implement] {Object[]} Optional parameter defines where to implement traits from
  2. 2. definition {Object} The description of the class to be created
Returns Classify.Class

Classify.Class.addProperty

Classify.Class.addProperty(name[, property][, prefix=""])
Adds a new property to the object's prototype of base.
Arguments
  1. 1. name {String|Object} The property name to add or if object is passed in then it will iterate through it to add properties
  2. 2. [property] {Object} The property to add to the class
  3. 3. [prefix=""] {String} Prefix of the property name if any
Returns Classify.Class

Classify.Class.removeProperty

Classify.Class.removeProperty(name)
Removes a property from the object's prototype or base.
Arguments
  1. 1. name {String} The name of the property to remove
Returns Classify.Class

Classify.Class.addStaticProperty

Classify.Class.addStaticProperty(name, property)
Adds a static property to the object's base.
Arguments
  1. 1. name {String} The name of the property to add
  2. 2. property {Object} The property to store into the object's base
Returns Classify.Class

Classify.Class.removeStaticProperty

Classify.Class.removeStaticProperty(name)
Removes a static property from the object's base.
Arguments
  1. 1. name {String} The name of the property to remove
Returns Classify.Class

Classify.Class.addBoundProperty

Classify.Class.addBoundProperty(name, property)
Adds a context bound property to the object's prototype.
Arguments
  1. 1. name {String} The name of the property to add
  2. 2. property {Function} The property to always bind the object's context with
Returns Classify.Class

Classify.Class.removeBoundProperty

Classify.Class.removeBoundProperty(name)
Removes a context bound property from the object's base.
Arguments
  1. 1. name {String} The name of the property to remove
Returns Classify.Class

Classify.Class.addObservableProperty

Classify.Class.addObservableProperty(name, property)
Adds a new observable property to the object's prototype.
Arguments
  1. 1. name {String} The name of the observable property to add
  2. 2. property {Object} The descriptor of the observable property
Returns Classify.Class

Classify.Class.removeObservableProperty

Classify.Class.removeObservableProperty(name)
Removes a observable property to the object's prototype.
Arguments
  1. 1. name {String} The name of the observable property to remove
Returns Classify.Class

Classify.Class.addAliasedProperty

Classify.Class.addAliasedProperty(name, property)
Adds a aliased property to the object's prototype based on a existing prototype method.
Arguments
  1. 1. name {String} The name of the alias for the new property
  2. 2. property {String} The name of the property alias
Returns Classify.Class

Classify.Class.addUnwrappedProperty

Classify.Class.addUnwrappedProperty(name, property)
Adds a property to the object's prototype that is not wrapped in the parent method wrapper.
Arguments
  1. 1. name {String} The name of the new property
  2. 2. property {String} The name of the property to add
Returns Classify.Class

Classify.Class.prototype

Classify.Class.prototype
Prototype chain for Classify.Class.
Object Prototype chain for Classify.Class

Classify.Class.prototype.constructor

Classify.Class.prototype.constructor
Reference to the constructor function of this object.
Classify.Class Reference to the constructor function of this object

Classify.Class.prototype.self

Classify.Class.prototype.self
Reference to the constructor function of this object.
Classify.Class Reference to the constructor function of this object

Classify.Class.prototype.init

Classify.Class.prototype.init()
True constructor method for this object, will be called when object is called with the "new" keyword.
Returns Classify.Class

Classify.Class.prototype.parent

Classify.Class.prototype.parent()
Internal reference property for methods that override a parent method, allow for access to the parent version of the function.
Returns Object

Classify.Class.prototype.extend

Classify.Class.prototype.extend([implement], definition)
Creates a new class that is a child of the current class.
Arguments
  1. 1. [implement] {Object[]} Optional parameter defines where to implement traits from
  2. 2. definition {Object} The description of the class to be created
Returns Classify.Class

Classify.Class.prototype.invoke

Classify.Class.prototype.invoke(name, args)
Magic method that can invoke any of the parent methods.
Arguments
  1. 1. name {Object} The name of the parent method to invoke
  2. 2. args {Array} The arguments to pass through to invoke
Returns Object

Classify.Namespace

Classify.Namespace

Classify.Namespace(name)
Namespace container that hold a tree of classes.
Constructor method
Extends Classify.Class
Arguments
  1. 1. name {String} The name of the namespace to construct with

Classify.Namespace.prototype

Classify.Namespace.prototype
Prototype chain for Classify.Namespace.
Classify.Class Prototype chain for Classify.Namespace

Classify.Namespace.prototype.name

Classify.Namespace.prototype.name
The name of the namespace.
String The name of the namespace

Classify.Namespace.prototype.ref

Classify.Namespace.prototype.ref
Hashtable containing references to all the classes created within this namespace.
Object Hashtable containing references to all the classes created within this namespace

Classify.Namespace.prototype.create

Classify.Namespace.prototype.create(name[, parent][, implement], definition)
Creates a new class within this namespace.
Arguments
  1. 1. name {String} The name of the created class within the namespace
  2. 2. [parent] {String|Classify.Class} Optional second parameter defines what object to inherit from, can be a string referencing a class within any namespace
  3. 3. [implement] {Object[]} Optional third parameter defines where to implement traits from
  4. 4. definition {Object} The description of the class to be created
Returns Classify.Class

Classify.Namespace.prototype.destroy

Classify.Namespace.prototype.destroy(classname)
Removes a defined class from this namespace and it's children classes.
Arguments
  1. 1. classname {String} Name of class to remove from this namespace
Returns Classify.Namespace

Classify.Namespace.prototype.exists

Classify.Namespace.prototype.exists(classname)
Checks if a class exists within this namespace.
Arguments
  1. 1. classname {String} Name of class to check if it has already been defined
Returns Boolean

Classify.Namespace.prototype.get

Classify.Namespace.prototype.get(name)
Attempt to retrieve a class within this namespace or the global one.
Arguments
  1. 1. name {String} The name of the class to retrieve

Classify.Namespace.prototype.load

Classify.Namespace.prototype.load(name)
Default loader function that loads the internal classes from.
Arguments
  1. 1. name {String} The name of the class to load
Returns Classify.Namespace

Classify.Namespace.prototype.setAutoloader

Classify.Namespace.prototype.setAutoloader(callback)
Sets the internal autoloader by overriding the Classify.Namespace.prototype.load method.
Arguments
  1. 1. callback {Function} The function to call when a class that doesn't exist needs to be loaded
Returns Classify.Namespace

Classify.Namespace.prototype.getName

Classify.Namespace.prototype.getName()
Gets the name of this namespace.

Classify.Namespace.prototype.toString

Classify.Namespace.prototype.toString()
Gets the translated toString name of this object "[namespace Name]".

Classify.Observer

Classify.Observer

Classify.Observer(value, value.value[, value.writable=true][, value.delay=0][, value.getter][, value.setter])
Wrapper object that allows for getter/setter/event listeners of object properties.
Constructor method
Extends Classify.Class
Arguments
  1. 1. value {Object} The internal value can be either an object or a value
  2. 2. value.value {Object} The internal value if the parameter was passed in as an object
  3. 3. [value.writable=true] {Boolean} Marks this object as writable or readonly
  4. 4. [value.delay=0] {Number} Only fire the event emitter after a delay of value.delay ms
  5. 5. [value.getter] {Function} The internal get modifier
  6. 6. [value.setter] {Function} The internal set modifier

Classify.Observer.prototype

Classify.Observer.prototype
Prototype chain for Classify.Observer.
Classify.Class Prototype chain for Classify.Observer

Classify.Observer.prototype.context

Classify.Observer.prototype.context
The context that this object is created within.
Classify.Class The context that this object is created within

Classify.Observer.prototype.name

Classify.Observer.prototype.name
The name of the property that this object observes.
String The name of the property that this object observes

Classify.Observer.prototype.writable

Classify.Observer.prototype.writable
Flag to check if this property is writable.
Boolean Flag to check if this property is writable

Classify.Observer.prototype.delay

Classify.Observer.prototype.delay
Number of seconds to delay the event emitter, 0 will disable delays.
Number Number of seconds to delay the event emitter, 0 will disable delays

Classify.Observer.prototype._debounce

Classify.Observer.prototype._debounce
Flag to hold the delay timer.
Number Flag to hold the delay timer

Classify.Observer.prototype.value

Classify.Observer.prototype.value
The internal value of this object.
Object The internal value of this object

Classify.Observer.prototype.events

Classify.Observer.prototype.events
Array containing all the event listeners for when this value changes.
Array Array containing all the event listeners for when this value changes

Classify.Observer.prototype.getter

Classify.Observer.prototype.getter(value)
Internal getter method that modifies the internal value being returned by the Classify.Observer.prototype.get method.
Arguments
  1. 1. value {Object} The internal value of this object
Returns Object

Classify.Observer.prototype.setter

Classify.Observer.prototype.setter(value, original)
Internal setter method that modifies the internal value being set by the Classify.Observer.prototype.set method.
Arguments
  1. 1. value {Object} The new value that will be set
  2. 2. original {Object} The original internal value of this object
Returns Object

Classify.Observer.prototype.get

Classify.Observer.prototype.get()
Gets the value of the internal property.
Returns Object

Classify.Observer.prototype.set

Classify.Observer.prototype.set(value)
Sets the value of the internal property.
Arguments
  1. 1. value {Object} Mixed value to store internally
Returns Classify.Class

Classify.Observer.prototype.emit

Classify.Observer.prototype.emit()
Starts the timers to call the registered event listeners.
Returns Classify.Class

Classify.Observer.prototype._triggerEmit

Classify.Observer.prototype._triggerEmit(args)
Fires the event listeners in the order they were added.
Arguments
  1. 1. args {Array} Array of arguments to pass to the bound event listeners

Classify.Observer.prototype.addListener

Classify.Observer.prototype.addListener(listener)
Add an event listener for when the internal value is changed.
Arguments
  1. 1. listener {Function} The event listener to add
Returns Classify.Class

Classify.Observer.prototype.on

Classify.Observer.prototype.on(listener)
Add an event listener for when the internal value is changed, alias to addListener.
Arguments
  1. 1. listener {Function} The event listener to add
Returns Classify.Class

Classify.Observer.prototype.once

Classify.Observer.prototype.once(listener)
Add an event listener to be called only once when the internal value is changed.
Arguments
  1. 1. listener {Function} The event listener to add
Returns Classify.Class

Classify.Observer.prototype.removeListener

Classify.Observer.prototype.removeListener(listener)
Remove an event listener from being fired when the internal value is changed.
Arguments
  1. 1. listener {Function} The event listener to remove
Returns Classify.Class

Classify.Observer.prototype.removeAllListeners

Classify.Observer.prototype.removeAllListeners()
Remove all event listeners from this object.
Returns Classify.Class

Classify.Observer.prototype.listeners

Classify.Observer.prototype.listeners()
Returns the array of internal listeners.
Returns Array

Classify.Observer.prototype.toValue

Classify.Observer.prototype.toValue()
Returns the internal value of this object in the scalar form.
Returns Boolean|Number|String

Classify.Observer.prototype.toString

Classify.Observer.prototype.toString()
Returns the special name of this object.
Returns String

Change Log

v0.10.2
  • Fix requirejs and amd definitions, remove async autoloader to support it
v0.10.1
  • Android Mobile thinks typeof RegExp == "function"
v0.10.0
  • Allow init mutators to override constructor return value & throw on scalar values
  • Adding null value to the prototype for observable properties
  • Observer properties don't need to go through objectDefineProperty
v0.9.10
  • BREAKING CHANGE, implements overrides extends when defining a class that institutes both
  • Use ES5 Object.create when possible
v0.9.9
  • Fix issue when extending non Classify objects overriding special properties
  • Global namespace is now a property of Classify on Classify.global
  • Updated Observers to have a 'on' alias and be able to bind events to be fired only once
  • Using Object.prototype.hasOwnProperty instead of .hasOwnProperty
v0.9.8
  • The called context is passed in as argument 1 for auto bound functions
v0.9.7
  • Refactoring internals so that adding and removing mutators can be externalized
  • Added mutator to auto bind functions to a class instance
v0.9.6
  • Fix case where classes are added as properties of other classes
v0.9.5
  • Added ability to add non wrapped methods to the class prototype
v0.9.4
  • Adding Travis-ci config file
  • Fix issue with IE6-9 when extending from non Classify classes
  • General bug fixes, removing support for IE<6
v0.9.3
  • Renaming Class.Extend to Class.extend
v0.9.2
  • Ability to delay event listeners from being triggered in observers
v0.9.0
  • Ability to add statics and observers in a container
v0.8.5
  • Fix issue with adding overriding functions to a parent prototype
v0.8.0
  • Only fire observer listeners when the value has changed
v0.7.5
  • fixing parent invocation using applicate in default invoke method
v0.7.0
  • Ability to inherit from objects that are not created with Classify.create.
  • Allow Classify to be AMD compatible.
  • extend utility function that "extends" objects with properties from other objects in a shallow manner
  • fix issue with class invocation of "invoke" when class is a direct property of a namespace
  • "get" in namespace cascades into "GLOBAL" namespace classes
  • groundwork for ability to extend native and non create objects
  • Removing check for native objects (IE doesn't allow extending native objects).
  • throw Error objects instead of strings
v0.6.0
  • better detection of parameters for using Classify function ("namespace/classname" for retrieval)
  • minification optimizations
  • quicker access to the GLOBAL namespace
v0.4.0
  • ability to inherit the parent invoke method if not defined
  • exporting provide to the Classify object
  • GLOBAL namespace that all namespaced classes can inherit from
v0.3.5
  • ability to get a class definition synchronously and asynchronously in Classify
  • ability to test if a namespace exists
  • adding Classify.noConflict for conflict free usage of Classify
v0.3.0
  • using more standardized nomenclature
  • "_construct_" => "init"
  • "_invoke_" => "invoke"
  • "_apply_" => "applicate"
  • "_parent_" => "parent"
  • "_self_" => "self"
v0.2.0
  • ability to create and destory classes created within namespaces
  • better checking of enumerable keys in IE < 9
  • bugfix on Extend functionality
  • Classify function endpoint has ability to to get namespace and create classes within the namespace
  • fixing issue with adding multiple properties to a class with the addProperty call
  • "get" in namespace takes in a callback and passes in the class
  • global references
  • namespacing functionality
  • only export classify to root object if it's a browser
  • pulled out the body of invoke to apply and default invoke will call apply
  • the exported object is created using the internal Create method
  • updated to "use strict";