Created
January 14, 2016 19:18
-
-
Save wgottschalk/d5a85631df52692d7730 to your computer and use it in GitHub Desktop.
An implementation of the new Keyword in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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