Last active
December 25, 2015 05:49
-
-
Save lvivski/6927077 to your computer and use it in GitHub Desktop.
This is how javascripts `new` operator works
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) { | |
if (typeof constructor !== 'function') { | |
throw new TypeError("'"+ constructor +"' is not a constructor") | |
} | |
var obj = {} | |
obj.__proto__ = constructor.prototype | |
var res = constructor.apply(obj, [].slice.call(arguments, 1)) | |
if (typeof res === "object") { | |
return res | |
} | |
return obj | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment