Skip to content

Instantly share code, notes, and snippets.

@Havvy
Last active December 14, 2015 05:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Havvy/5037770 to your computer and use it in GitHub Desktop.
Save Havvy/5037770 to your computer and use it in GitHub Desktop.
// These are also the same function as below, with slightly different syntax.
function new (constructor, args) {
var binding = Object.create(constructor.prototype);
var retval = constructor.apply(binding, args);
if (retval === null || typeof retval !== 'object') {
return binding;
}
return retval;
}
function new (constructor, args) {
var binding = Object.create(constructor.prototype);
var retval = constructor.apply(binding, args);
if (retval === null || typeof retval !== 'object') {
return binding;
} else {
return retval;
}
}
@Havvy
Copy link
Author

Havvy commented Mar 28, 2013

Given a constructor Foo, new Foo(a, b, c) would be the same as new(Foo, [a, b, c]).

I say would be, because new is a reserved word, so the function doesn't actually compile.

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