MicroNova SANSHO.JS is a tiny public-domain Javascript library based on the notion of class object, code-named after a "tiny but hot" kind of Japanese pepper.

1. Core

1.1. Class Object

In SANSHO.JS, a class object C is a regular Javascript object that supports the following methods:

C.newInstance(config): returns a newly created instance of C initialized by optional config object.

C.isClassOf(x): returns true if and only if x is an instance of C.

For example,

// x is an instance of C
var x = C.newInstance();
// so the following is true
console.debug(C.isClassOf(x));
// and the following is false
console.debug(C.isClassOf({}));

A class object C always has the following three properties (class triad):

C._self: regular Javascript object which is the prototype of all instances of C.

C._constructor: regular Javascript constructor function of all instances of C.

C._class: C itself.

These class triad properties are also available in C._self and C._constructor, so for example (C._self)._class = C, and x._class = C for any instance x of C since C._self is the prototype of x (x._class = C._self._class = C).

Note that class triad properties are all system properties (non-configurable, non-writable, non-enumerable).

1.2. Extension

A class object D is an extension of another class object C when:

C is the regular Javascript prototype of D, and _super property of D is set to C.

C._self is the regular Javascript protoype of D._self, and _super property of D._self is set to C._self.

_super property of D._constructor is set to C._constructor

Each SANSHO class object C has C.extend(extender) method to create its extension class object which takes an optional extender function as follows:

var D = C.extend
(
function(_self, _class)
{
// this is extender function that takes two arguments _self and _class
// _self argument is the object to become D._self
// _self._super is C._self
// _class argument is the object to become D
// _class._super is C
// this is C here
// set up _self and _class objects here
}
);

SANSHO.JS uses standard Javascript function closure for super-class access via _self and _class function arguments, allowing each method function to have access to the class hierarchy on creation. This makes SANSHO.JS class extension mechanism to be tiny and yet flexible.

Super-class methods can be called using standard Javascript call or apply method on this object as follows:

Note that it is not possible to create instances of D inside the extender function because D._constructor is not set yet. In order to do post-extension class object modification, use D.modify(modifier) as follows:

var D = C.extend
(
// no extender needed here if constructor is not modified (see below)
)
.modify
(
// modify D
function(_self, _class)
{
// this is modifier function with the same arguments as extender
// _self and _class can be modified
// also new instances of _class can be created
// this is D here
_class.defaultInstance = _class.newInstance();
}
);

For the default class object SANSHO, its _super properties are all undefined (SANSHO._super, SANSHO._self._super, and SANSHO._constructor._super).

1.3. Constructor

For a SANSHO class object C, C._constructor is created by C.makeConstructor() method, which by default returns the following function:

SANSHO._self.init is defined as an empty function, so by default this.init above (= SANSHO._self.init) does nothing. However, init (or any other instance method) can be overridden either at class level or at instance level:

TYPE._object.iterateChain(chainProperty, source, f, target): iterate through given property chain (e.g., '_super', 'nextSibling', etc.) starting from source until the chain is terminated by a "falsy" value.

Each iteration method above returns optional target argument after iteration by default, and takes the following iteration function f as an argument:

function(source, key, target)
{
// source: source object given to the iteration method
// key: iterating key (property name, index, or current object in case of iterateProperty)
// target: target argument given to the iteration method
// return nothing from this function to continue iteration till the end
// and return target after iteration
// or return any value (not undefined) here to break out of
// iteration and return this value instead of default target
}