This commit is contained in:
Ilya Kantor 2019-08-03 00:01:38 +03:00
parent 25a77d376a
commit 0757d51080
2 changed files with 36 additions and 24 deletions

View file

@ -63,7 +63,7 @@ Object.defineProperty(obj, propertyName, descriptor)
```
`obj`, `propertyName`
: The object and property to work on.
: The object and its property to apply the descriptor.
`descriptor`
: Property descriptor to apply.
@ -116,7 +116,7 @@ Object.defineProperty(user, "name", {
});
*!*
user.name = "Pete"; // Error: Cannot assign to read only property 'name'...
user.name = "Pete"; // Error: Cannot assign to read only property 'name'
*/!*
```
@ -126,25 +126,24 @@ Now no one can change the name of our user, unless they apply their own `defineP
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.
```
Here's the same operation, but for the case when a property doesn't exist:
Here's the same example, but the property is created from scratch:
```js run
let user = { };
Object.defineProperty(user, "name", {
*!*
value: "Pete",
value: "John",
// for new properties need to explicitly list what's true
enumerable: true,
configurable: true
*/!*
});
alert(user.name); // Pete
user.name = "Alice"; // Error
alert(user.name); // John
user.name = "Pete"; // Error
```
## Non-enumerable
Now let's add a custom `toString` to `user`.