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
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
@@ -126,25 +126,24 @@ Now no one can change the name of our user, unless they apply their own `defineP
126
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.
127
127
```
128
128
129
-
Here's the same operation, but for the case when a property doesn't exist:
129
+
Here's the same example, but the property is created from scratch:
130
130
131
131
```js run
132
132
let user = { };
133
133
134
134
Object.defineProperty(user, "name", {
135
135
*!*
136
-
value:"Pete",
136
+
value:"John",
137
137
// for new properties need to explicitly list what's true
Copy file name to clipboardExpand all lines: 1-js/07-object-properties/02-property-accessors/article.md
+30-17Lines changed: 30 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -34,7 +34,7 @@ let user = {
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,21 @@ 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
+
На данный момент `fullName` имеет только геттер. Если попытаться присвоить значение свойству `user.fullName`, то это вызовет ошибку:
61
+
62
+
```js run
63
+
let user = {
64
+
getfullName() {
65
+
return`...`;
66
+
}
67
+
};
68
+
69
+
*!*
70
+
user.fullName="Test"; // Error (property has only a getter)
71
+
*/!*
72
+
```
59
73
60
74
Let's fix it by adding a setter for `user.fullName`:
61
75
@@ -84,13 +98,8 @@ alert(user.surname); // Cooper
84
98
85
99
As the result, we have a "virtual" property `fullName`. It is readable and writable, but in fact does not exist.
86
100
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 property any more.
89
-
90
-
- If there's a getter -- we can read `object.prop`, otherwise we can't.
91
-
- If there's a setter -- we can set `object.prop=...`, otherwise we can't.
92
-
93
-
And in either case we can't `delete` an accessor property.
101
+
```smart header="No support for `delete`"
102
+
An attempt to `delete` on accessor property causes an error.
94
103
```
95
104
96
105
@@ -100,7 +109,7 @@ Descriptors for accessor properties are different -- as compared with data prope
100
109
101
110
For accessor properties, there is no `value` and `writable`, but instead there are `get` and `set` functions.
102
111
103
-
So an accessor descriptor may have:
112
+
That is, an accessor descriptor may have:
104
113
105
114
- **`get`** -- a function without arguments, that works when a property is read,
106
115
- **`set`** -- a function with one argument, that is called when the property is set,
@@ -132,7 +141,7 @@ alert(user.fullName); // John Smith
132
141
for(let key in user) alert(key); // name, surname
133
142
```
134
143
135
-
Please note once again that a property can be either an accessor or a data property, not both.
144
+
Please note once again that a property can be either an accessor (has `get/set` methods) or a data property (has a `value`), not both.
136
145
137
146
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.
163
+
Getters/setters can be used as wrappers over "real" property values to gain more control over operations with them.
155
164
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:
165
+
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
166
158
167
```js run
159
168
let user = {
@@ -176,14 +185,16 @@ alert(user.name); // Pete
176
185
user.name=""; // Name is too short...
177
186
```
178
187
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.
188
+
So, the name is stored in `_name` property, and the access is done via getter and setter.
189
+
190
+
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
191
181
192
182
193
## Using for compatibility
183
194
184
-
One of the great ideas behind getters and setters -- they allow to take control over a "regular" data property at any moment by replacing it with getter and setter and tweak its behavior.
195
+
One of the great uses of accessors -- they allow to take control over a "regular" data property at any moment by replacing it with getter and setter and tweak its behavior.
185
196
186
-
Let's say we started implementing user objects using data properties `name` and `age`:
197
+
Imagine, we started implementing user objects using data properties `name` and `age`:
187
198
188
199
```js
189
200
functionUser(name, age) {
@@ -209,7 +220,9 @@ let john = new User("John", new Date(1992, 6, 1));
209
220
210
221
Now what to do with the old code that still uses `age` property?
211
222
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/used by many other people. And besides, `age` is a nice thing to have in `user`, right? In some places it's just what we want.
223
+
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