Skip to content
Myr

complex-objects.md

This file

Path

website/docs/complex-objects.md

75 lines · 2.5 KiB · Markdown

Last changed by

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

History of this file

Take it

docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95

lines 1–6why, one step back
1---
2id: complex-objects3title: Classes4---56<center>

chore: docusaurus upgradeMichel Weststratebefore adoption48a91b3

line 7why, one step back
7<div data-ea-publisher="immerjs" data-ea-type="image" className="horizontal bordered"></div>

docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95

lines 8–9why, one step back
8</center>
9

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 10–11why, one step back
10By default, Immer does not strictly handle Plain object's non-eumerable properties such as getters/setters for performance reason. If you want this behavior to be strict, you can opt-in with `useStrictShallowCopy(true)`.
11

docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95

lines 12–63why, one step back
12Plain 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.
1314```js15import {immerable} from "immer"1617class Foo {18 [immerable] = true // Option 11920 constructor() {21 this[immerable] = true // Option 222 }23}2425Foo[immerable] = true // Option 326```2728### Example2930```js31import {immerable, produce} from "immer"3233class Clock {34 [immerable] = true3536 constructor(hour, minute) {37 this.hour = hour38 this.minute = minute39 }4041 get time() {42 return `${this.hour}:${this.minute}`43 }4445 tick() {46 return produce(this, draft => {47 draft.minute++48 })49 }50}5152const clock1 = new Clock(12, 10)53const clock2 = clock1.tick()54console.log(clock1.time) // 12:1055console.log(clock2.time) // 12:1156console.log(clock2 instanceof Clock) // true57```5859### Semantics in detail6061The semantics on how classes are drafted are as follows:62631. A draft of a class is a fresh object but with the same prototype as the original object.

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 64–69why, one step back
641. When creating a draft, Immer will copy all _own_ properties from the base to the draft.This includes non-enumerable and symbolic properties.
651. _Own_ getters will be invoked during the copy process, just like `Object.assign` would.661. Inherited getters and methods will remain as is and be inherited by the draft.671. Immer will not invoke constructor functions.681. The final instance will be constructed with the same mechanism as the draft was created.691. Only getters that have a setter as well will be writable in the draft, as otherwise the value can't be copied back.

docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95

lines 70–72why, one step back
70
71Because 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.72

docs: clarify that Map / Set cant be subclassed or made immerable. Fixes #781Michel Weststratebefore adoption0af1143

line 73why, one step back
73Immer does not support exotic / engine native objects such as DOM Nodes or Buffers, nor is subclassing Map, Set or arrays supported and the `immerable` symbol can't be used on them.

docs: Update to docusaurus 2Michel Weststratebefore adoption42aac95

lines 74–75why, one step back
74
75So when working for example with `Date` objects, you should always create a new `Date` instance instead of mutating an existing `Date` object.