Skip to content

Instantly share code, notes, and snippets.

@jwerle
Created January 5, 2013 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwerle/4463863 to your computer and use it in GitHub Desktop.
Save jwerle/4463863 to your computer and use it in GitHub Desktop.
.new method like Ruby for Javascript
/**
* With some magic getters/setters you can achieve similar syntax like Ruby
**/
function foo() { console.log('im foo') }
foo.__defineGetter__('new', function(){ return new foo })
f = foo.new
// >> im foo
void console.log((new foo) instanceof foo)
// >> im foo
// >> true
void console.log((foo.new) instanceof foo)
// >> im foo
// >> true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment