Skip to content

Commit e8091b1

Browse files
committed
closes #1309
1 parent 6dd2798 commit e8091b1

File tree

1 file changed

+16
-8
lines changed
  • 1-js/07-object-properties/01-property-descriptors

1 file changed

+16
-8
lines changed

1-js/07-object-properties/01-property-descriptors/article.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ In this chapter we'll study additional configuration options, and in the next we
1111

1212
Object properties, besides a **`value`**, have three special attributes (so-called "flags"):
1313

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.
1515
- **`enumerable`** -- if `true`, then listed in loops, otherwise not listed.
1616
- **`configurable`** -- if `true`, the property can be deleted and these attributes can be modified, otherwise not.
1717

@@ -100,9 +100,9 @@ Compare it with "normally created" `user.name` above: now all flags are falsy. I
100100

101101
Now let's see effects of the flags by example.
102102

103-
## Read-only
103+
## Non-writable
104104

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:
106106

107107
```js run
108108
let user = {
@@ -123,7 +123,7 @@ user.name = "Pete"; // Error: Cannot assign to read only property 'name'
123123
Now no one can change the name of our user, unless they apply their own `defineProperty` to override ours.
124124

125125
```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.
127127
```
128128

129129
Here's the same example, but the property is created from scratch:
@@ -194,9 +194,9 @@ alert(Object.keys(user)); // name
194194

195195
The non-configurable flag (`configurable:false`) is sometimes preset for built-in objects and properties.
196196

197-
A non-configurable property can not be deleted or altered with `defineProperty`.
197+
A non-configurable property can not be deleted.
198198

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:
200200

201201
```js run
202202
let descriptor = Object.getOwnPropertyDescriptor(Math, 'PI');
@@ -219,7 +219,15 @@ Math.PI = 3; // Error
219219
// delete Math.PI won't work either
220220
```
221221

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.
223231

224232
Here we are making `user.name` a "forever sealed" constant:
225233

@@ -237,7 +245,7 @@ Object.defineProperty(user, "name", {
237245
// all this won't work:
238246
// user.name = "Pete"
239247
// delete user.name
240-
// defineProperty(user, "name", ...)
248+
// defineProperty(user, "name", { value: "Pete" })
241249
Object.defineProperty(user, "name", {writable: true}); // Error
242250
*/!*
243251
```

0 commit comments

Comments
 (0)