Function.prototype.construct
by rwaldron
JavaScript
// From: http://twitter.com/#!/BrendanEich/status/89823462847291392
Function.prototype.construct = function() {
return new(
this.bind.apply( this, [,].concat( [].slice.call(arguments) ) )
);
};
// Example constructor that "does things"
function Ctor( options ) {
this.whoami = "Ctor";
this.settings = options;
return this;
}
var c = Ctor.construct( {} );
console.log( c );
/*
Ctor
settings: {}
whoami: "Ctor"
*/