@@ -152,18 +152,16 @@ objects represent some thing which could change over time, a value represents
152
152
the state of that thing at a particular instance of time. This principle is most
153
153
important to understanding the appropriate use of immutable data. In order to
154
154
treat Immutable.js collections as values, it's important to use the
155
- ` Immutable.is() ` function or ` .equals() ` method to determine value equality
156
- instead of the ` === ` operator which determines object reference identity.
155
+ ` Immutable.is() ` function or ` .equals() ` method to determine * value equality*
156
+ instead of the ` === ` operator which determines object * reference identity* .
157
157
158
158
<!-- runkit:activate -->
159
159
``` js
160
160
const { Map } = require (' immutable' )
161
161
const map1 = Map ({ a: 1 , b: 2 , c: 3 })
162
- const map2 = map1 .set (' b' , 2 )
163
- assert .equal (map1, map2) // uses map1.equals
164
- assert .strictEqual (map1, map2) // uses ===
165
- const map3 = map1 .set (' b' , 50 )
166
- assert .notEqual (map1, map3) // uses map1.equals
162
+ const map2 = Map ({ a: 1 , b: 2 , c: 3 })
163
+ map1 .equals (map2) // true
164
+ map1 === map2 // false
167
165
```
168
166
169
167
Note: As a performance optimization Immutable.js attempts to return the existing
@@ -174,6 +172,14 @@ which would prefer to re-run the function if a deeper equality check could
174
172
potentially be more costly. The ` === ` equality check is also used internally by
175
173
` Immutable.is ` and ` .equals() ` as a performance optimization.
176
174
175
+ <!-- runkit:activate -->
176
+ ``` js
177
+ const { Map } = require (' immutable' )
178
+ const map1 = Map ({ a: 1 , b: 2 , c: 3 })
179
+ const map2 = map1 .set (' b' , 2 ) // Set to same value
180
+ map1 === map2 // true
181
+ ```
182
+
177
183
If an object is immutable, it can be "copied" simply by making another reference
178
184
to it instead of copying the entire object. Because a reference is much smaller
179
185
than the object itself, this results in memory savings and a potential boost in
@@ -182,8 +188,8 @@ execution speed for programs which rely on copies (such as an undo-stack).
182
188
<!-- runkit:activate -->
183
189
``` js
184
190
const { Map } = require (' immutable' )
185
- const map1 = Map ({ a: 1 , b: 2 , c: 3 })
186
- const clone = map1;
191
+ const map = Map ({ a: 1 , b: 2 , c: 3 })
192
+ const mapCopy = map; // Look, "copies" are free!
187
193
```
188
194
189
195
[ React ] : http://facebook.github.io/react/
@@ -279,11 +285,11 @@ shorthand, while Immutable Maps accept keys of any type.
279
285
const { fromJS } = require (' immutable' )
280
286
281
287
const obj = { 1 : " one" }
282
- Object .keys (obj) // [ "1" ]
283
- assert . equal (obj[" 1" ], obj[1 ]) // "one" === "one"
288
+ console . log ( Object .keys (obj) ) // [ "1" ]
289
+ console . log (obj[" 1" ], obj[1 ]) // "one", "one"
284
290
285
291
const map = fromJS (obj)
286
- assert . notEqual (map .get (" 1" ), map .get (1 )) // "one" !== undefined
292
+ console . log (map .get (" 1" ), map .get (1 )) // "one", undefined
287
293
```
288
294
289
295
Property access for JavaScript Objects first converts the key to a string, but
0 commit comments