Created
December 11, 2013 23:32
-
-
Save rauschma/7920504 to your computer and use it in GitHub Desktop.
This is roughly how one would implement the `new` operator in JavaScript.
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
function newOperator(Constr, args) { | |
var inst = Object.create(Constr.prototype); | |
var result = Constr.apply(inst, args); | |
if (typeof result === 'object' && result !== null) { | |
return result; | |
} | |
return inst; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Make that
if (typeof result === 'function' || (typeof result === 'object' && result !== null)) {