Created
January 5, 2013 21:48
-
-
Save jwerle/4463863 to your computer and use it in GitHub Desktop.
.new method like Ruby for Javascript
This file contains 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
/** | |
* 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