Created
January 28, 2013 21:37
-
-
Save ianb/4659318 to your computer and use it in GitHub Desktop.
Thinking about what the new operator really looks like, if you treat Object.create() as the more fundamental operation
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
function new_(constructor /* arguments to constructor */) { | |
var args = Array.prototype.slice.call(arguments, 1); | |
var obj = Object.create(constructor.prototype); | |
var result = constructor.apply(obj, args); | |
if (result === undefined) { | |
result = obj; | |
} | |
return result; | |
} | |
// Equivalent: | |
// new Constructor(a, b) | |
// new_(Constructor, a, b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment