This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const friends1 = #["G. Lestrade"] | |
| friends2 = friends1.pushed("Dr. Watson") | |
| // tup1 == #["G. Lestrade"] | |
| // tup2 == #["G. Lestrade", "Dr. Watson"] | |
| const tup3 = #["G. Lestrade", "Dr. Watson"] | |
| const tup4 = tup2.sorted | |
| // tup3 == #["G. Lestrade", "Dr. Watson"] | |
| // tup4 == #["Dr. Watson", "G. Lestrade"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const user = #{ name: "S. Holmes", age: 27 } | |
| // with immutable types, you are restricted to changing value directly: | |
| user.age = 28 // throws error | |
| // but, this works: | |
| const userOneYearOlder = #{ ...user, age: 28} // => #{ name: "S. Holmes", age: 28 } | |
| // It also means: | |
| user == userOneYearOlder // => false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const primitive = "string is an immutable primitive" | |
| const primitive2 = primitive + " - change it" | |
| primitive !== primitive2 | |
| const obj1 = { value: 1} | |
| const obj2 = obj1 | |
| obj2.value = 2 // change | |
| obj1 === obj2 // => true | |
| // Restricted use: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const friends1 = #["G. Lestrade", "John Hamish Watson"] | |
| const friends2 = #["G. Lestrade", "John Hamish Watson"] | |
| friends1 == friends2 // => true | |
| // But | |
| const friends1 = #["G. Lestrade", "John Hamish Watson"] | |
| const friends2 = #["John Hamish Watson", "G. Lestrade"] | |
| friends1 == friends2 // => false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const user1 = { name: "S. Holmes", age: 27 } | |
| const user2 = { age: 27, name: "S. Holmes"} | |
| user1 == user2 // => true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // value equality | |
| const user = #{ name: "Dr. Watson"} | |
| const user2 = #{ name: "Dr. Watson"} | |
| const user2 == user // => true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Identity equality | |
| const user = { name: "Dr. Watson"} | |
| const userRef = user | |
| userRef == user // => true | |
| const user = { name: "Dr. Watson"} | |
| const user2 = { name: "Dr. Watson"} | |
| const user2 == user // => false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Immutable.JS | |
| const record1 = Record({ a: 1, b: 2 }) | |
| const record2 = record1.set('b', 50) | |
| const b = record2.get('b') | |
| const recordA = Record({ a: 1, b: 2 }) | |
| const recordB = Record({ a: 1, b: 2 }) | |
| recordA.equals(recordB) //true | |
| recordA == (recordB) //false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // nested primitive and immutable type | |
| const record = #{ name: "Primitive value", number: 33, nestedRecord : #{ number: 50} } | |
| // nested primitive and immutable type | |
| const tuple = #[ "Primitive value", 33, #{ number: 50}, #[1, true, null] } | |
| // record with a nested object instead of record throws an error as it's not a primitive type anymore | |
| const record = #{ name: "Primitive value", number: 33, nestedObject: { number: 50} } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // the compound value: | |
| // consists of two primitive type | |
| const object = { name: "Primitive value", number: 33 } | |
| // consists of two primitive types & one function | |
| const object = { name: "Primitive value", number: 33, fn: () => {} } |