Skip to content

Commit 0757d51

Browse files
committed
fixes
1 parent 25a77d3 commit 0757d51

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Object.defineProperty(obj, propertyName, descriptor)
6363
```
6464

6565
`obj`, `propertyName`
66-
: The object and property to work on.
66+
: The object and its property to apply the descriptor.
6767

6868
`descriptor`
6969
: Property descriptor to apply.
@@ -116,7 +116,7 @@ Object.defineProperty(user, "name", {
116116
});
117117

118118
*!*
119-
user.name = "Pete"; // Error: Cannot assign to read only property 'name'...
119+
user.name = "Pete"; // Error: Cannot assign to read only property 'name'
120120
*/!*
121121
```
122122

@@ -126,25 +126,24 @@ Now no one can change the name of our user, unless they apply their own `defineP
126126
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.
127127
```
128128

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

131131
```js run
132132
let user = { };
133133

134134
Object.defineProperty(user, "name", {
135135
*!*
136-
value: "Pete",
136+
value: "John",
137137
// for new properties need to explicitly list what's true
138138
enumerable: true,
139139
configurable: true
140140
*/!*
141141
});
142142

143-
alert(user.name); // Pete
144-
user.name = "Alice"; // Error
143+
alert(user.name); // John
144+
user.name = "Pete"; // Error
145145
```
146146

147-
148147
## Non-enumerable
149148

150149
Now let's add a custom `toString` to `user`.

1-js/07-object-properties/02-property-accessors/article.md

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ let user = {
3434
};
3535
```
3636

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

3939
```js run
4040
let user = {
@@ -55,7 +55,21 @@ alert(user.fullName); // John Smith
5555

5656
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.
5757

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+
get fullName() {
65+
return `...`;
66+
}
67+
};
68+
69+
*!*
70+
user.fullName = "Test"; // Error (property has only a getter)
71+
*/!*
72+
```
5973

6074
Let's fix it by adding a setter for `user.fullName`:
6175

@@ -84,13 +98,8 @@ alert(user.surname); // Cooper
8498

8599
As the result, we have a "virtual" property `fullName`. It is readable and writable, but in fact does not exist.
86100

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.
94103
```
95104
96105
@@ -100,7 +109,7 @@ Descriptors for accessor properties are different -- as compared with data prope
100109
101110
For accessor properties, there is no `value` and `writable`, but instead there are `get` and `set` functions.
102111
103-
So an accessor descriptor may have:
112+
That is, an accessor descriptor may have:
104113
105114
- **`get`** -- a function without arguments, that works when a property is read,
106115
- **`set`** -- a function with one argument, that is called when the property is set,
@@ -132,7 +141,7 @@ alert(user.fullName); // John Smith
132141
for(let key in user) alert(key); // name, surname
133142
```
134143

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

137146
If we try to supply both `get` and `value` in the same descriptor, there will be an error:
138147

@@ -151,9 +160,9 @@ Object.defineProperty({}, 'prop', {
151160

152161
## Smarter getters/setters
153162

154-
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.
155164

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`:
157166

158167
```js run
159168
let user = {
@@ -176,14 +185,16 @@ alert(user.name); // Pete
176185
user.name = ""; // Name is too short...
177186
```
178187

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

181192

182193
## Using for compatibility
183194

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

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`:
187198

188199
```js
189200
function User(name, age) {
@@ -209,7 +220,9 @@ let john = new User("John", new Date(1992, 6, 1));
209220

210221
Now what to do with the old code that still uses `age` property?
211222

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?
224+
225+
Let's keep it.
213226

214227
Adding a getter for `age` solves the problem:
215228

0 commit comments

Comments
 (0)