Skip to content

Commit fa4665b

Browse files
committed
closes #2306
1 parent db4a880 commit fa4665b

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

1-js/04-object-basics/01-object/article.md

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -92,30 +92,6 @@ let user = {
9292
```
9393
That is called a "trailing" or "hanging" comma. Makes it easier to add/remove/move around properties, because all lines become alike.
9494

95-
````smart header="Object with const can be changed"
96-
Please note: an object declared as `const` *can* be modified.
97-
98-
For instance:
99-
100-
```js run
101-
const user = {
102-
name: "John"
103-
};
104-
105-
*!*
106-
user.name = "Pete"; // (*)
107-
*/!*
108-
109-
alert(user.name); // Pete
110-
```
111-
112-
It might seem that the line `(*)` would cause an error, but no. The `const` fixes the value of `user`, but not its contents.
113-
114-
The `const` would give an error only if we try to set `user=...` as a whole.
115-
116-
There's another way to make constant object properties, we'll cover it later in the chapter <info:property-descriptors>.
117-
````
118-
11995
## Square brackets
12096

12197
For multiword properties, the dot access doesn't work:

1-js/04-object-basics/02-object-copy/article.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,32 @@ alert(*!*user.name*/!*); // 'Pete', changes are seen from the "user" reference
6161
6262
The example above demonstrates that there is only one object. As if we had a cabinet with two keys and used one of them (`admin`) to get into it. Then, if we later use another key (`user`) we can see changes.
6363
64+
## Const objects can be modified
65+
66+
There's another important "side effect" of storing objects as references.
67+
68+
An object declared as `const` can be modified.
69+
70+
For instance:
71+
72+
```js run
73+
const user = {
74+
name: "John"
75+
};
76+
77+
*!*
78+
user.name = "Pete"; // (*)
79+
*/!*
80+
81+
alert(user.name); // Pete
82+
```
83+
84+
It might seem that the line `(*)` would cause an error, but no. The `const` fixes the value of `user`, so it must always reference the same object. But it says nothing about properties of that object.
85+
86+
In other words, the `const user` gives an error only if we try to set `user=...` as a whole.
87+
88+
If we really need to make constant object properties, that's also possible, but with totally different methods, we'll cover them later in the chapter <info:property-descriptors>.
89+
6490
## Comparison by reference
6591

6692
The equality `==` and strict equality `===` operators for objects work exactly the same.

0 commit comments

Comments
 (0)