Feb 23, 2017 · 1 min read
I really like your article, but I was wondering if this code actually worked as expected for you:
// main.js
import Greeter from './greeter';
import Gesturer from './gesturer';
import ConsoleOutputStream from './console-output-stream';const consoleOutputStream = ConsoleOutputStream();
const greeter = Greeter(consoleOutputStream);
const gesturer = Gesturer(consoleOutputStream);const wavingGreeter = Object.assign({}, greeter, gesturer);wavingGreeter.message = 'Salutations Earth.';
wavingGreeter.greet(); // prints Salutations Earth.
wavingGreeter.wave(); // prints *Waves hand*
For me, the getter and setter is lost when doing Object.assign() . If I want to set the message on the composed object, I have to copy the getter / setter manually like this:
const wavingGreeter = Object.assign({}, greeter, gesturer);
Object.defineProperty(
wavingGreeter,
'message',
Object.getOwnPropertyDescriptor(greeter, 'message')
)That is easy to forget, would be nice to be able to do it like in your example.