Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Forked from gf3/LOLheritence.js
Created September 29, 2010 14:37
Show Gist options
  • Save rwaldron/602856 to your computer and use it in GitHub Desktop.
Save rwaldron/602856 to your computer and use it in GitHub Desktop.

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!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment