fiber.js | |
|---|---|
| ( function( global ) { |
Baseline setup | |
| Stores whether the object is being initialized. i.e., whether
to run the | var initializing = false, |
| Keep a few prototype references around - for speed access, and saving bytes in the minified version. | ArrayProto = Array.prototype, |
| Save the previous value of | previousFiber = global.Fiber; |
| Helper function to copy properties from one object to the other. | function copy( from, to ) {
var name;
for( name in from ) {
if( from.hasOwnProperty( name ) ) {
to[name] = from[name];
}
}
} |
| The base | function Fiber(){}; |
ExtendReturns a subclass. | Fiber.extend = function( fn ) { |
| Keep a reference to the current prototye. | var parent = this.prototype, |
| Invoke the function which will return an object literal used to define the prototype. Additionally, pass in the parent prototype, which will allow instances to use it. | properties = fn( parent ), |
| Stores the constructor's prototype. | proto; |
| The constructor function for a subclass. | function child(){
if( !initializing ){ |
| Custom initialization is done in the | this.init.apply( this, arguments ); |
| Prevent susbsequent calls to | this.init = void 0;
}
} |
| Instantiate a base class (but only create the instance, without running | initializing = true;
proto = child.prototype = new this;
initializing = false; |
| Add default | proto.init = function(){
if ( typeof parent.init === 'function' ) {
parent.init.apply( this, arguments );
}
}; |
| Copy the properties over onto the new prototype. | copy( properties, proto ); |
| Enforce the constructor to be what we expect. | proto.constructor = child; |
| Keep a reference to the parent prototype. (Note: currently used by decorators and mixins, so that the parent can be inferred). | child.__base__ = parent; |
| Make this class extendable. | child.extend = Fiber.extend;
return child;
}; |
Utilities | |
ProxyReturns a proxy object for accessing base methods with a given context.
Overloads:
| Fiber.proxy = function( base, instance ) {
var name,
iface = {},
wrap; |
| If there's only 1 argument specified, then it is the instance,
thus infer | if ( arguments.length === 1 ) {
instance = base;
base = instance.constructor.__base__;
} |
| Returns a function which calls another function with | wrap = function( fn ) {
return function() {
return base[fn].apply( instance, arguments );
};
}; |
| For each function in | for( name in base ){
if( base.hasOwnProperty( name ) && typeof base[name] === 'function' ){
iface[name] = wrap( name );
}
}
return iface;
}; |
DecorateDecorate an instance with given decorator(s).
Note: when a decorator is executed, the argument passed in is the super class' prototype,
and the context (i.e. the Example usage: | Fiber.decorate = function( instance /*, decorator[s] */) {
var i, |
| Get the base prototype. | base = instance.constructor.__base__, |
| Get all the decorators in the arguments. | decorators = ArrayProto.slice.call( arguments, 1 ),
len = decorators.length;
for( i = 0; i < len; i++ ){
copy( decorators[i].call( instance, base ), instance );
}
}; |
MixinAdd functionality to a Fiber definition
Note: when a mixing is executed, the argument passed in is the super class' prototype (i.e., the base) Overloads:
Example usage: | Fiber.mixin = function( definition /*, mixin[s] */ ) {
var i, |
| Get the base prototype. | base = definition.__base__, |
| Get all the mixins in the arguments. | mixins = ArrayProto.slice.call( arguments, 1 ),
len = mixins.length;
for( i = 0; i < len; i++ ){
copy( mixins[i]( base ), definition.prototype );
}
}; |
noConflictRun Fiber.js in noConflict mode, returning the | Fiber.noConflict = function() {
global.Fiber = previousFiber;
return Fiber;
}; |
Common JS | |
| Export | if( typeof module !== 'undefined' ) {
if( typeof module.setExports === 'function' ) {
module.setExports( Fiber );
} else if( module.exports ) {
module.exports = Fiber;
}
} else {
global.Fiber = Fiber;
} |
| Establish the root object: | })( this );
|