Skip to content

Commit 9241e39

Browse files
committed
Add Freeze An Object, Sorta as a javascript til
1 parent 6b2e7be commit 9241e39

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
1010
For a steady stream of TILs from a variety of rocketeers, checkout
1111
[til.hashrocket.com](https://til.hashrocket.com/).
1212

13-
_586 TILs and counting..._
13+
_587 TILs and counting..._
1414

1515
---
1616

@@ -217,6 +217,7 @@ _586 TILs and counting..._
217217
- [Default And Named Exports From The Same Module](javascript/default-and-named-exports-from-the-same-module.md)
218218
- [Enable ES7 Transforms With react-rails](javascript/enable-es7-transforms-with-react-rails.md)
219219
- [Expand Emojis With The Spread Operator](javascript/expand-emojis-with-the-spread-operator.md)
220+
- [Freeze An Object, Sorta](javascript/freeze-an-object-sorta.md)
220221
- [Globally Install A Package With Yarn](javascript/globally-install-a-package-with-yarn.md)
221222
- [Immutable Remove With The Spread Operator](javascript/immutable-remove-with-the-spread-operator.md)
222223
- [Initialize A New JavaScript Project With Yarn](javascript/initialize-a-new-javascript-project-with-yarn.md)

javascript/freeze-an-object-sorta.md

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Freeze An Object, Sorta
2+
3+
You can freeze a JavaScript object using `Object.freeze` which will help
4+
enforce some immutability practices. Don't be fooled though, you can still
5+
modify arrays and objects in the frozen object.
6+
7+
Here is what the docs have to say:
8+
9+
> The Object.freeze() method freezes an object: that is, prevents new
10+
> properties from being added to it; prevents existing properties from being
11+
> removed; and prevents existing properties, or their enumerability,
12+
> configurability, or writability, from being changed, it also prevents the
13+
> prototype from being changed.
14+
15+
And here is `Object.freeze` in action:
16+
17+
```javascript
18+
> const things = {one: "two", hello: "world", cats: ["Von Neumann", "Sosa"]}
19+
undefined
20+
> Object.freeze(things)
21+
{one: "two", hello: "world", cats: Array(2)}
22+
23+
> things.one = "three"
24+
"three"
25+
> things.dogs = []
26+
[]
27+
> delete things.hello
28+
false
29+
30+
> things
31+
{one: "two", hello: "world", cats: Array(2)}
32+
33+
> things.cats.push("Sneaky")
34+
3
35+
36+
> things
37+
{one: "two", hello: "world", cats: Array(3)}
38+
```
39+
40+
See the [MDN
41+
Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze)
42+
for more details.
43+
44+
h/t Jake Worth

0 commit comments

Comments
 (0)