Skip to content

Instantly share code, notes, and snippets.

@gf3
Created September 29, 2010 03:46
Show Gist options
  • Save gf3/602266 to your computer and use it in GitHub Desktop.
Save gf3/602266 to your computer and use it in GitHub Desktop.
Executable child instances

Proof of Concept

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!
@fearphage
Copy link

And Opera too...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment