Black magic! Executable child instances!
Tested with nodejs.
/* ------------------------------ Main Class ------------------------------ */ | |
// Returns "instances" of itself which are actually functions. | |
function Ben ( greeting ) { var Parent, scope | |
function Scope () { | |
// Here is where you put your normal constructor junk | |
this.greeting = greeting | |
this.colours = [ 'yellow', 0xFFFFFF ] | |
} | |
// Magic | |
function Proxy() { | |
Scope.prototype.init.apply( scope, arguments ) | |
} | |
Parent = arguments.callee | |
function Dummy() {} | |
Dummy.prototype = Parent.prototype | |
Scope.prototype = new Dummy() | |
scope = new Scope() | |
Proxy.__proto__ = scope | |
// Go time! | |
return Proxy | |
} | |
Ben.prototype.init = function( name ) { | |
console.log( this.greeting + ' ' + name + '!' ) | |
} | |
Ben.prototype.lol = function() { | |
console.log( '"' + this.greeting + '" is silly!' ) | |
} | |
/* ------------------------------ Demo ------------------------------ */ | |
var b = Ben( 'hai' ) | |
console.log( b.greeting ) // hai | |
console.log( b.colours ) // [ 'yellow', 16777215 ] | |
b( 'Ben' ) // hai Ben! | |
b.lol() // "hai" is silly! | |
needs moar ie compatibility
And Opera too...
that is sassy