Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save leobalter/1074144 to your computer and use it in GitHub Desktop.
Save leobalter/1074144 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"
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment