complex-objects.md
This file
Path
docs/complex-objects.md
71 lines · 2.1 KiB · Markdown
Last changed by
Some dotsMichel Weststratebefore adoption4e438d0
History of this fileTake it
Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 1–2why, one step back1---2id: complex-objectsSome dotsMichel Weststratebefore adoption4e438d0
line 3why, one step back3title: ClassesMerge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 4–7why, one step back4---56<div id="codefund"><!-- fallback content --></div>7Some dotsMichel Weststratebefore adoption4e438d0
line 8why, one step back8Plain objects (objects without a prototype), arrays, `Map`s and `Set`s are always drafted by Immer. Every other object must use the `immerable` symbol to mark itself as compatible with Immer. When one of these objects is mutated within a producer, its prototype is preserved between copies.Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 9why, one step back9Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
lines 10–23why, one step back10```js11import {immerable} from "immer"1213class Foo {14 [immerable] = true // Option 11516 constructor() {17 this[immerable] = true // Option 218 }19}2021Foo[immerable] = true // Option 322```23Some dotsMichel Weststratebefore adoption4e438d0
line 24why, one step back24### ExampleMerge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
line 25why, one step back25Some dotsMichel Weststratebefore adoption4e438d0
lines 26–27why, one step back26```js27import {immerable, produce} from "immer"Merge pull request #421 from immerjs/docsMichel Weststratebefore adoption9be6e46
line 28why, one step back28Some dotsMichel Weststratebefore adoption4e438d0
lines 29–30why, one step back29class Clock {30 [immerable] = truefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 31why, one step back31Some dotsMichel Weststratebefore adoption4e438d0
lines 32–71why, one step back32 constructor(hour, minute) {33 this.hour = hour34 this.minute = minute35 }3637 get time() {38 return `${this.hour}:${minute}`39 }4041 tick() {42 return produce(this, draft => {43 draft.minute++44 })45 }46}4748const clock1 = new Clock(12, 10)49const clock2 = clock1.tick()50console.log(clock1.time) // 12:1051console.log(clock2.time) // 12:1152console.log(clock2 instanceof Clock) // true53```5455### Semantics in detail5657The semantics on how classes are drafted are as follows:58591. A draft of a class is a fresh object but with the same prototype as the original object.602. When creating a draft, Immer will copy all _own_ properties from the base to the draft.This includes non-enumerable and symbolic properties.613. _Own_ getters will be invoked during the copy process, just like `Object.assign` would.624. Inherited getters and methods will remain as is and be inherited by the draft.635. Immer will not invoke constructor functions.646. The final instance will be constructed with the same mechanism as the draft was created.657. Only getters that have a setter as well will be writable in the draft, as otherwise the value can't be copied back.6667Because Immer will dereference own getters of objects into normal properties, it is possible to use objects that use getter/setter traps on their fields, like MobX and Vue do.6869Immer does not support exotic objects such as DOM Nodes or Buffers.7071So when working for example with `Date` objects, you should always create a new `Date` instance instead of mutating an existing `Date` object.