Skip to content

Instantly share code, notes, and snippets.

@AjayPoshak
Created July 1, 2019 14:05
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save AjayPoshak/8e091a7a470d9d3ffbc1ac7f31edbaa2 to your computer and use it in GitHub Desktop.
Function to implement new keyword
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