You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/07-object-properties/01-property-descriptors/article.md
+16-8Lines changed: 16 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,7 @@ In this chapter we'll study additional configuration options, and in the next we
11
11
12
12
Object properties, besides a **`value`**, have three special attributes (so-called "flags"):
13
13
14
-
-**`writable`** -- if `true`, can be changed, otherwise it's read-only.
14
+
-**`writable`** -- if `true`, the value can be changed, otherwise it's read-only.
15
15
-**`enumerable`** -- if `true`, then listed in loops, otherwise not listed.
16
16
-**`configurable`** -- if `true`, the property can be deleted and these attributes can be modified, otherwise not.
17
17
@@ -100,9 +100,9 @@ Compare it with "normally created" `user.name` above: now all flags are falsy. I
100
100
101
101
Now let's see effects of the flags by example.
102
102
103
-
## Read-only
103
+
## Non-writable
104
104
105
-
Let's make `user.name`read-only by changing `writable` flag:
105
+
Let's make `user.name`non-writable (can't be reassigned) by changing `writable` flag:
106
106
107
107
```js run
108
108
let user = {
@@ -123,7 +123,7 @@ user.name = "Pete"; // Error: Cannot assign to read only property 'name'
123
123
Now no one can change the name of our user, unless they apply their own `defineProperty` to override ours.
124
124
125
125
```smart header="Errors appear only in strict mode"
126
-
In the non-strict mode, no errors occur when writing to read-only properties and such. But the operation still won't succeed. Flag-violating actions are just silently ignored in non-strict.
126
+
In the non-strict mode, no errors occur when writing to non-writable properties and such. But the operation still won't succeed. Flag-violating actions are just silently ignored in non-strict.
127
127
```
128
128
129
129
Here's the same example, but the property is created from scratch:
@@ -194,9 +194,9 @@ alert(Object.keys(user)); // name
194
194
195
195
The non-configurable flag (`configurable:false`) is sometimes preset for built-in objects and properties.
196
196
197
-
A non-configurable property can not be deleted or altered with `defineProperty`.
197
+
A non-configurable property can not be deleted.
198
198
199
-
For instance, `Math.PI` is read-only, non-enumerable and non-configurable:
199
+
For instance, `Math.PI` is non-writable, non-enumerable and non-configurable:
200
200
201
201
```js run
202
202
let descriptor =Object.getOwnPropertyDescriptor(Math, 'PI');
@@ -219,7 +219,15 @@ Math.PI = 3; // Error
219
219
// delete Math.PI won't work either
220
220
```
221
221
222
-
Making a property non-configurable is a one-way road. We cannot change it back, because `defineProperty` doesn't work on non-configurable properties.
222
+
Making a property non-configurable is a one-way road. We cannot change it back with `defineProperty`.
223
+
224
+
To be precise, non-configurability imposes several restrictions on `defineProperty`:
225
+
1. Can't change `configurable` flag.
226
+
2. Can't change `enumerable` flag.
227
+
3. If a property is non-writable, then can't make it writable or change value.
228
+
4. Can't change `get/set` for an accessor property.
229
+
230
+
So, few things about the property still can be changed. E.g. a value may change, if the property is writable. The idea is that we can't change flags of a non-configurable property.
223
231
224
232
Here we are making `user.name` a "forever sealed" constant:
0 commit comments