Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Created July 9, 2011 23:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rwaldron/1074049 to your computer and use it in GitHub Desktop.
Save rwaldron/1074049 to your computer and use it in GitHub Desktop.
// 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"
*/
// 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"
*/
@rwaldron
Copy link
Author

rwaldron commented Jul 9, 2011

Function.prototype.construct:
http://jsfiddle.net/rwaldron/dVMpD/

Function.prototype.build:
http://jsfiddle.net/rwaldron/snhrF/

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