Skip to content

Instantly share code, notes, and snippets.

@ianb
Created January 28, 2013 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianb/4659318 to your computer and use it in GitHub Desktop.
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
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