Created
July 9, 2011 23:28
-
-
Save rwaldron/1074049 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From @littlecalculist | |
// http://twitter.com/#!/littlecalculist/status/89840539494662144 | |
Function.prototype.build = function(a) { | |
return new ( | |
this.bind.apply(this, [,].concat(a)) | |
); | |
}; | |
// Example constructor that "does things" | |
function Ctor( options ) { | |
this.whoami = "Ctor"; | |
this.settings = options; | |
return this; | |
} | |
var c = Ctor.build( {} ); | |
console.log( c ); | |
/* | |
Ctor | |
settings: {} | |
whoami: "Ctor" | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From @brendaneich | |
// http://twitter.com/#!/BrendanEich/status/89823462847291392 | |
Function.prototype.construct = function() { | |
return new( | |
this.bind.apply( this, [,].concat( [].slice.call(arguments) ) ) | |
); | |
}; | |
function Ctor( options ) { | |
this.whoami = "Ctor"; | |
this.settings = options; | |
return this; | |
} | |
var c = Ctor.construct( {} ); | |
console.log( c ); | |
/* | |
Ctor | |
settings: {} | |
whoami: "Ctor" | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Function.prototype.construct:
http://jsfiddle.net/rwaldron/dVMpD/
Function.prototype.build:
http://jsfiddle.net/rwaldron/snhrF/