Skip to content

Instantly share code, notes, and snippets.

@lvivski
Last active December 25, 2015 05:49
Show Gist options
  • Save lvivski/6927077 to your computer and use it in GitHub Desktop.
Save lvivski/6927077 to your computer and use it in GitHub Desktop.
This is how javascripts `new` operator works
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