-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimmutable-objects.js
More file actions
51 lines (36 loc) · 1.1 KB
/
immutable-objects.js
File metadata and controls
51 lines (36 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/** Object.feeze */
// takes an object, makes it immutable
// no new properties, cannot update existing properties
// returns the same object
{
const originalObject = {
foo: "bar",
};
const frozenObject = Object.freeze(originalObject);
originalObject.alpha = "omega";
originalObject.foo = "buzz";
console.log(originalObject);
console.log(frozenObject === originalObject); // true
/** Object.isFrozen */
// we can check whether an object is frozen with Object.isFrozen
console.log(Object.isFrozen(frozenObject)); //true
}
/** Object.seal */
// takes in an object
// cannot add new properties but can update existing properties
// returns the same object
{
const originalObject = {
foo: "bar",
};
const sealedObject = Object.seal(originalObject);
// wont work
sealedObject.alpha = "omega";
// works
sealedObject.foo = "buzz";
console.log(sealedObject); // {foo:'buzz'}
console.log(sealedObject === originalObject); // true
/** Object.isSealed */
// we can check whether an object is sealed with Object.isSealed
console.log(Object.isSealed(sealedObject)); // true
}