testing Constructor Inheritece

Also includes a native event emitter.

by imcrthy

HTML

<script src="http://ud-cdn.com/js/lodash/1.0.0/lodash.min.js"></script>
<div id="foo"></div>

JavaScript

Object.defineProperty( Function.prototype, 'namespace', { 
    'enumerable': true,
    'value': {
        set: function( name ) {
            this._namespace = this._namespace || [];
            if( this._namespace.indexOf( name ) < 0 ) { this._namespace.push( name ); }        
        },
        get: function() {
            return this._namespace.join( '.' );
        }
    }
});

Object.defineProperty( Function.prototype, 'id', { 
    'enumerable': true,
    'value': {
        
        set: function( id ) {
            this._id = this._id || [];
            if( this._id.indexOf( id ) < 0 ) { this._id.push( id ); }
        },
        get: function() { return this._id.join( '.' ); }
    }
});


function ServiceBus( id, config ) {
    this.id = id || this.constructor.name;
    this.namespace = arguments.callee.name;
    this.config = config || this.config || {};
   
    // Testing setting Function-specific property
    this.channel = {
        path: this.namespace
    };
    
    if( this.debug ) {
     console.log( 'arguments.callee.name', arguments.callee.name );
     console.log( 'this.constructor.name', this.constructor.name );
     console.log( 'this instanceof arguments.callee', this instanceof arguments.callee );
     console.log( 'Object.getPrototypeOf( this )', Object.getPrototypeOf( this ) ); 
     console.log( 'arguments.callee.prototype', arguments.callee.prototype ); 
    }
        
    // Detect non-standard instantiation - not using "new"
    if( !( this instanceof arguments.callee ) ) {
        // console.debug( 'Notce:', arguments.callee.name, 'instantiated via', this.constructor.name, 'context.' );
    
        // Recover correct constructor prototype and expose
        for( var name in arguments.callee.prototype ) {            
            Object.defineProperty( this, name, Object.getOwnPropertyDescriptor( arguments.callee.prototype, name ) );
        };

    } 
    
    return this;
}

Object.defineProperties(ServiceBus, {
    create: {
       ...