Skip to content

Instantly share code, notes, and snippets.

@mpj
Created September 18, 2016 14:08
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mpj/2a6bd3ec1f9d327a1692b7bb56ad4e76 to your computer and use it in GitHub Desktop.
Save mpj/2a6bd3ec1f9d327a1692b7bb56ad4e76 to your computer and use it in GitHub Desktop.
Code from Fun Fun Function YouTube episode: The 'new' keyword - Object Creation in JavaScript P3
function Person(saying) {
this.saying = saying
}
Person.prototype.talk = function() {
console.log('I say:', this.saying)
}
function spawn(constructor) {
var obj = {}
Object.setPrototypeOf(obj, constructor.prototype)
var argsArray = Array.prototype.slice.apply(arguments)
return constructor.apply(obj, argsArray.slice(1)) || obj
}
var crockford = spawn(Person, 'SEMICOLANS!!!1one1')
crockford.talk()
@gustiando
Copy link

@mpj wouldn't it make sense to call var argsArray = Array.prototype.slice.apply(arguments, [1]) instead since it binds and executes? I might be missing an important lesson here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment