Created
August 19, 2014 02:54
-
-
Save audibleblink/583be9b750a0e40302ea to your computer and use it in GitHub Desktop.
Reimplementation of the `new` keyword as a function
This file contains hidden or 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
var newNew = function(constructor, args) { | |
var instance = Object.create(constructor.prototype) | |
// instance.__proto__ = constructor.prototype // Same as line above | |
instance.constructor = constructor // So that you can see who created this. | |
constructor.apply(instance, args) // Same as #call except args is an arrray with apply | |
return instance | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment