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
: The object and its property to apply the descriptor.
67
67
68
68
`descriptor`
69
-
: Property descriptor to apply.
69
+
: Property descriptor object to apply.
70
70
71
71
If the property exists, `defineProperty` updates its flags. Otherwise, it creates the property with the given value and flags; in that case, if a flag is not supplied, it is assumed `false`.
72
72
@@ -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:
user.name="Pete"; // Error: Cannot assign to read only property 'name'...
119
+
user.name="Pete"; // Error: Cannot assign to read only property 'name'
120
120
*/!*
121
121
```
122
122
123
123
Now no one can change the name of our user, unless they apply their own `defineProperty` to override ours.
124
124
125
-
Here's the same operation, but for the case when a property doesn't exist:
125
+
```smart header="Errors appear only in strict mode"
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
+
```
128
+
129
+
Here's the same example, but the property is created from scratch:
126
130
127
131
```js run
128
132
let user ={};
129
133
130
134
Object.defineProperty(user, "name",{
131
135
*!*
132
-
value:"Pete",
133
-
// for new properties need to explicitly list what's true
136
+
value:"John",
137
+
// for new properties we need to explicitly list what's true
134
138
enumerable:true,
135
139
configurable:true
136
140
*/!*
137
141
});
138
142
139
-
alert(user.name); //Pete
140
-
user.name="Alice"; // Error
143
+
alert(user.name); //John
144
+
user.name="Pete"; // Error
141
145
```
142
146
143
-
144
147
## Non-enumerable
145
148
146
149
Now let's add a custom `toString` to `user`.
147
150
148
-
Normally, a built-in `toString` for objects is non-enumerable, it does not show up in `for..in`. But if we add `toString` of our own, then by default it shows up in `for..in`, like this:
151
+
Normally, a built-in `toString` for objects is non-enumerable, it does not show up in `for..in`. But if we add a `toString` of our own, then by default it shows up in `for..in`, like this:
149
152
150
153
```js run
151
154
let user ={
@@ -159,7 +162,7 @@ let user ={
159
162
for (let key in user) alert(key); // name, toString
160
163
```
161
164
162
-
If we don't like it, then we can set `enumerable:false`. Then it won't appear in `for..in` loop, just like the built-in one:
165
+
If we don't like it, then we can set `enumerable:false`. Then it won't appear in a `for..in` loop, just like the built-in one:
163
166
164
167
```js run
165
168
let user ={
@@ -191,9 +194,9 @@ alert(Object.keys(user)); // name
191
194
192
195
The non-configurable flag (`configurable:false`) is sometimes preset for built-in objects and properties.
193
196
194
-
A non-configurable property can not be deleted or altered with `defineProperty`.
197
+
A non-configurable property can not be deleted.
195
198
196
-
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:
197
200
198
201
```js run
199
202
let descriptor =Object.getOwnPropertyDescriptor(Math, 'PI');
@@ -216,7 +219,13 @@ Math.PI = 3; // Error
216
219
// delete Math.PI won't work either
217
220
```
218
221
219
-
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. Can't change `writable: false` to `true` (the other way round works).
228
+
4. Can't change `get/set` for an accessor property (but can assign them if absent).
220
229
221
230
Here we are making `user.name` a "forever sealed" constant:
```smart header="Errors appear only in use strict"
243
-
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.
251
+
```smart header="\"Non-configurable\" doesn't mean \"non-writable\""
252
+
Notable exception: a value of non-configurable, but writable property can be changed.
253
+
254
+
The idea of `configurable: false` is to prevent changes to property flags and its deletion, not changes to its value.
244
255
```
245
256
246
257
## Object.defineProperties
@@ -298,13 +309,13 @@ Property descriptors work at the level of individual properties.
298
309
There are also methods that limit access to the *whole* object:
Copy file name to clipboardExpand all lines: 1-js/07-object-properties/02-property-accessors/article.md
+32-26Lines changed: 32 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@
3
3
4
4
There are two kinds of properties.
5
5
6
-
The first kind is *data properties*. We already know how to work with them. Actually, all properties that we've been using till now were data properties.
6
+
The first kind is *data properties*. We already know how to work with them. All properties that we've been using until now were data properties.
7
7
8
8
The second type of properties is something new. It's *accessor properties*. They are essentially functions that work on getting and setting a value, but look like regular properties to an external code.
9
9
@@ -27,14 +27,14 @@ The getter works when `obj.propName` is read, the setter -- when it is assigned.
27
27
28
28
For instance, we have a `user` object with `name` and `surname`:
29
29
30
-
```js run
30
+
```js
31
31
let user ={
32
32
name:"John",
33
33
surname:"Smith"
34
34
};
35
35
```
36
36
37
-
Now we want to add a "fullName" property, that should be "John Smith". Of course, we don't want to copy-paste existing information, so we can implement it as an accessor:
37
+
Now we want to add a `fullName` property, that should be `"John Smith"`. Of course, we don't want to copy-paste existing information, so we can implement it as an accessor:
38
38
39
39
```js run
40
40
let user ={
@@ -55,7 +55,19 @@ alert(user.fullName); // John Smith
55
55
56
56
From outside, an accessor property looks like a regular one. That's the idea of accessor properties. We don't *call*`user.fullName` as a function, we *read* it normally: the getter runs behind the scenes.
57
57
58
-
As of now, `fullName` has only a getter. If we attempt to assign `user.fullName=`, there will be an error.
58
+
As of now, `fullName` has only a getter. If we attempt to assign `user.fullName=`, there will be an error:
59
+
60
+
```js run
61
+
let user ={
62
+
getfullName(){
63
+
return`...`;
64
+
}
65
+
};
66
+
67
+
*!*
68
+
user.fullName="Test"; // Error (property has only a getter)
69
+
*/!*
70
+
```
59
71
60
72
Let's fix it by adding a setter for `user.fullName`:
61
73
@@ -82,25 +94,15 @@ alert(user.name); // Alice
82
94
alert(user.surname); // Cooper
83
95
```
84
96
85
-
Now we have a "virtual" property. It is readable and writable, but in fact does not exist.
86
-
87
-
```smart header="Accessor properties are only accessible with get/set"
88
-
Once a property is defined with `get prop()` or `set prop()`, it's an accessor property, not a data properety any more.
89
-
90
-
- If there's a getter -- we can read `object.prop`, othrewise we can't.
91
-
- If there's a setter -- we can set `object.prop=...`, othrewise we can't.
92
-
93
-
And in either case we can't `delete` an accessor property.
94
-
```
95
-
97
+
As the result, we have a "virtual" property `fullName`. It is readable and writable.
96
98
97
99
## Accessor descriptors
98
100
99
-
Descriptors for accessor properties are different -- as compared with data properties.
101
+
Descriptors for accessor properties are different from those for data properties.
100
102
101
-
For accessor properties, there is no `value`and`writable`, but instead there are `get` and `set` functions.
103
+
For accessor properties, there is no `value`or`writable`, but instead there are `get` and `set` functions.
102
104
103
-
So an accessor descriptor may have:
105
+
That is, an accessor descriptor may have:
104
106
105
107
-**`get`** -- a function without arguments, that works when a property is read,
106
108
-**`set`** -- a function with one argument, that is called when the property is set,
@@ -132,7 +134,7 @@ alert(user.fullName); // John Smith
132
134
for(let key in user) alert(key); // name, surname
133
135
```
134
136
135
-
Please note once again that a property can be either an accessor or a data property, not both.
137
+
Please note that a property can be either an accessor (has `get/set` methods) or a data property (has a `value`), not both.
136
138
137
139
If we try to supply both `get` and `value` in the same descriptor, there will be an error:
Getters/setters can be used as wrappers over "real" property values to gain more control over them.
156
+
Getters/setters can be used as wrappers over "real" property values to gain more control over operations with them.
155
157
156
-
For instance, if we want to forbid too short names for `user`, we can store `name` in a special property `_name`. And filter assignments in the setter:
158
+
For instance, if we want to forbid too short names for `user`, we can have a setter `name`and keep the value in a separate property `_name`:
157
159
158
160
```js run
159
161
let user ={
@@ -176,14 +178,16 @@ alert(user.name); // Pete
176
178
user.name=""; // Name is too short...
177
179
```
178
180
179
-
Technically, the external code may still access the name directly by using `user._name`. But there is a widely known agreement that properties starting with an underscore `"_"` are internal and should not be touched from outside the object.
181
+
So, the name is stored in `_name` property, and the access is done via getter and setter.
182
+
183
+
Technically, external code is able to access the name directly by using `user._name`. But there is a widely known convention that properties starting with an underscore `"_"` are internal and should not be touched from outside the object.
180
184
181
185
182
186
## Using for compatibility
183
187
184
-
One of the great ideas behind getters and setters -- they allow to take control over a "normal" data property and tweak it at any moment.
188
+
One of the great uses of accessors is that they allow to take control over a "regular" data property at any moment by replacing it with a getter and a setter and tweak its behavior.
185
189
186
-
For instance, we started implementing user objects using data properties `name` and `age`:
190
+
Imagine we started implementing user objects using data properties `name` and `age`:
187
191
188
192
```js
189
193
functionUser(name, age){
@@ -209,9 +213,11 @@ let john = new User("John", new Date(1992, 6, 1));
209
213
210
214
Now what to do with the old code that still uses `age` property?
211
215
212
-
We can try to find all such places and fix them, but that takes time and can be hard to do if that code is written by other people. And besides, `age` is a nice thing to have in `user`, right? In some places it's just what we want.
216
+
We can try to find all such places and fix them, but that takes time and can be hard to do if that code is used by many other people. And besides, `age` is a nice thing to have in `user`, right?
0 commit comments