Created
July 1, 2019 14:05
-
-
Save AjayPoshak/8e091a7a470d9d3ffbc1ac7f31edbaa2 to your computer and use it in GitHub Desktop.
Function to implement new keyword
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 newFunction(funcName, args) { | |
// Copy the function prototype to object | |
let obj = Object.create(funcName.prototype) | |
// Call constructor function with supplied arguments, and assign this to newly created object | |
funcName.call(obj, args) | |
// return newly created object | |
return obj | |
} | |
// Example | |
function Workshop(teacher) { | |
this.teacher = teacher | |
} | |
Workshop.prototype.ask = function ask(question) { | |
console.log(this.teacher, question) | |
} | |
const workshop = newFunction(Workshop, 'kyle') | |
workshop.ask('How are you??') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment