Created
April 13, 2019 17:24
-
-
Save winarcooo/22d2956a854539600b5ddd8f276f4b63 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Person(saying) { | |
this.saying = saying | |
} | |
Person.prototype.talk = function() { | |
console.log('I say:', this.saying); | |
} | |
// replicate what `new` does | |
function spawn(constructor) { | |
// 1. create a new object | |
var obj = {} | |
// 2. set the prototype | |
Object.setPrototypeOf(obj, constructor.prototype) | |
var argsArray = Array.prototype.slice.apply(arguments) | |
// 3. execute constructor with `this` | |
// 4. return the created object, unless the constructor return the object | |
return constructor.apply(obj, argsArray.slice(1)) || obj | |
} | |
var crockforg = spawn(Person, 'semicolans1!!') | |
crockforg.talk() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment