Search code examples
javascriptnode.jsobject-create

What does it mean to call Object.create(new EventEmitter) in Node.js


I've read the MDN document on Object.create. It only pointed out the scenario when the first argument is a prototype. However, I've seen some code in Node.js like this:

var events = require('events');  
var emitter = new events.EventEmitter();  
var a = Object.create(emitter);

So what does Object.create() do when its first argument is an object?


Solution

  • The first parameter to Object.create is always the prototype, which is always an object.

    In this case it just means that the prototype happens to be created via new - no big deal. If new does (as it should) return a new object, then think of it as a one-off (or "unshared") prototype that will only be used for the new Object.create'd object.

    The [prototype] of the Object.create prototype, as established by new, will also be part of the chain, as per standard rules.

    See Object.create on MDN:

    Object.create(proto [, propertiesObject ])

    proto - The object which should be the prototype of the newly-created object.