engineforce/ImmutableAssign

Support classes

Closed this issue · 2 comments

Hi, great library, I love the idea of not having to use any special structures to achieve immutable updates.

Would it be possible to also support ES6 classes when updating the object? Right now, if I have

class Hello {
    constructor(world) { this.world = world }
    say() { console.log(this.world) }
}
const a = new Hello("World")

and I update a, the resulting object no longer has the same __proto__, so I can't call say on it, etc.

At first glance, looks like this could be achieved by simply replacing the extend function you use with one that first creates the "destination" object with Object.create(Object.getPrototypeOf(value)) before assigning the properties to it.

in the quickCopy function, using new obj.constructor() to create instances of ES6 classes.

Object.create(Object.getPrototypeOf(value)) does not support ES6 collection types (i.e. Map, Set) because they use native constructors.

See this stackoverflow question.

BTW, if you want a test, you can use it
npm install https://github.com/Yaojian/ImmutableAssign.git --save before @engineforce merging the pull request.