Reputation: 18559
Trying to determine why this wont function properly.
// assuming this works
const { emitter } = window.project;
emitter.emit('foo');
// why doesnt this
const { emitter: { emit } } = window.project;
emit('foo');
Upvotes: 0
Views: 134
Reputation: 816322
Presumably because the emit
method depends on this
referring to a specific value. The value of this
depends on how a function is called (if it's an unbound, non-arrow function), and you are calling the function in two different ways:
emitter.emit()
will cause this
inside emit
to refer to emitter
emit()
will cause this
to refer to undefined
(strict mode) or the global object (non-strict mode)That has nothing to do with ES6 specifically.
More information about this
: You Don't Know JS: this & Object Prototypes.
Upvotes: 3