Skip to content

Instantly share code, notes, and snippets.

@rauschma
Created December 11, 2013 23:32
Show Gist options
  • Save rauschma/7920504 to your computer and use it in GitHub Desktop.
Save rauschma/7920504 to your computer and use it in GitHub Desktop.
This is roughly how one would implement the `new` operator in JavaScript.
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;
}
@olov
Copy link

olov commented Dec 12, 2013

Make that if (typeof result === 'function' || (typeof result === 'object' && result !== null)) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment