minor fixes

This commit is contained in:
Ilya Kantor 2021-07-22 23:47:13 +03:00
parent 614c85ad5d
commit 0082fbe7a3

View file

@ -194,7 +194,7 @@ alert(Object.keys(user)); // name
The non-configurable flag (`configurable:false`) is sometimes preset for built-in objects and properties. The non-configurable flag (`configurable:false`) is sometimes preset for built-in objects and properties.
A non-configurable property can not be deleted. A non-configurable property can't be deleted, its attributes can't be modified.
For instance, `Math.PI` is non-writable, non-enumerable and non-configurable: For instance, `Math.PI` is non-writable, non-enumerable and non-configurable:
@ -214,14 +214,23 @@ alert( JSON.stringify(descriptor, null, 2 ) );
So, a programmer is unable to change the value of `Math.PI` or overwrite it. So, a programmer is unable to change the value of `Math.PI` or overwrite it.
```js run ```js run
Math.PI = 3; // Error Math.PI = 3; // Error, because it has writable: false
// delete Math.PI won't work either // delete Math.PI won't work either
``` ```
We also can't change `Math.PI` to be `writable` again:
```js run
// Error, because of configurable: false
Object.defineProperty(Math, "PI", { writable: true });
```
There's absolutely nothing we can do with `Math.PI`.
Making a property non-configurable is a one-way road. We cannot change it back with `defineProperty`. Making a property non-configurable is a one-way road. We cannot change it back with `defineProperty`.
**The idea of "configurable: false" is to prevent changes of property flags and its deletion, while allowing to change its value.** **Please note: `configurable: false` prevents changes of property flags and its deletion, while allowing to change its value.**
Here `user.name` is non-configurable, but we can still change it (as it's writable): Here `user.name` is non-configurable, but we can still change it (as it's writable):
@ -238,7 +247,7 @@ user.name = "Pete"; // works fine
delete user.name; // Error delete user.name; // Error
``` ```
And here we make `user.name` a "forever sealed" constant: And here we make `user.name` a "forever sealed" constant, just like the built-in `Math.PI`:
```js run ```js run
let user = { let user = {
@ -258,9 +267,9 @@ Object.defineProperty(user, "name", { value: "Pete" });
``` ```
```smart header="The only attribute change possible: writable true -> false" ```smart header="The only attribute change possible: writable true -> false"
There's a minor exception, that we have to mention. There's a minor exception about changing flags.
We can change `writable: true` to `false` for a non-configurable property, thus making the property readonly (can add another layer of protection). But not the other way around. We can change `writable: true` to `false` for a non-configurable property, thus preventing its value modification (to add another layer of protection). Not the other way around though.
``` ```
## Object.defineProperties ## Object.defineProperties