Skip to content

Instantly share code, notes, and snippets.

@wgottschalk
Created January 14, 2016 19:18
Show Gist options
  • Save wgottschalk/d5a85631df52692d7730 to your computer and use it in GitHub Desktop.
Save wgottschalk/d5a85631df52692d7730 to your computer and use it in GitHub Desktop.
An implementation of the new Keyword in Javascript
function NEW(constructor, argsArray) {
var obj = {}; // step 1
obj.__proto__ = constructor.prototype; // step 2
constructor.apply(obj, argsArray); // step 3
return obj; // step 4
}
function Parent(name) {
this.name = name;
}
Parent.prototype.greet = function() {
console.log("Hi, I'm " + this.name);
}
var mom = NEW(Parent, ["Mom"]);
var dad = new Parent("Dad");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment