8000 Readme example updates. Fixes #1506 · immutable-js/immutable-js@901bd40 · GitHub
[go: up one dir, main page]

Skip to content

Commit 901bd40

Browse files
committed
Readme example updates. Fixes #1506
1 parent b29b484 commit 901bd40

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

README.md

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -152,18 +152,16 @@ objects represent some thing which could change over time, a value represents
152152
the state of that thing at a particular instance of time. This principle is most
153153
important to understanding the appropriate use of immutable data. In order to
154154
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*.
157157

158158
<!-- runkit:activate -->
159159
```js
160160
const { Map } = require('immutable')
161161
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
167165
```
168166

169167
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
174172
potentially be more costly. The `===` equality check is also used internally by
175173
`Immutable.is` and `.equals()` as a performance optimization.
176174

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+
177183
If an object is immutable, it can be "copied" simply by making another reference
178184
to it instead of copying the entire object. Because a reference is much smaller
179185
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).
182188
<!-- runkit:activate -->
183189
```js
184190
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!
187193
```
188194

189195
[React]: http://facebook.github.io/react/
@@ -279,11 +285,11 @@ shorthand, while Immutable Maps accept keys of any type.
279285
const { fromJS } = require('immutable')
280286

281287
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"
284290

285291
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
287293
```
288294

289295
Property access for JavaScript Objects first converts the key to a string, but

0 commit comments

Comments
 (0)
0