Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 800d47c

Browse files
committed
minor
1 parent 61bc426 commit 800d47c

File tree

6 files changed

+25
-23
lines changed

6 files changed

+25
-23
lines changed

‎1-js/08-prototypes/04-prototype-methods/article.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ let rabbit = Object.create(animal);
2626
*/!*
2727

2828
alert(rabbit.eats); // true
29+
2930
*!*
3031
alert(Object.getPrototypeOf(rabbit) === animal); // get the prototype of rabbit
3132
*/!*
@@ -78,7 +79,7 @@ As of now we have all these ways at our disposal.
7879

7980
Why was `__proto__` replaced by the functions `getPrototypeOf/setPrototypeOf`? That's an interesting question, requiring us to understand why `__proto__` is bad. Read on to get the answer.
8081

81-
```warn header="Don't reset `[[Prototype]]` unless the speed doesn't matter"
82+
```warn header="Don't change `[[Prototype]]` on existing objects if speed matters"
8283
Technically, we can get/set `[[Prototype]]` at any time. But usually we only set it once at the object creation time, and then do not modify: `rabbit` inherits from `animal`, and that is not going to change.
8384

8485
And JavaScript engines are highly optimized to that. Changing a prototype "on-the-fly" with `Object.setPrototypeOf` or `obj.__proto__=` is a very slow operation, it breaks internal optimizations for object property access operations. So evade it unless you know what you're doing, or JavaScript speed totally doesn't matter for you.
@@ -111,7 +112,7 @@ Here the consequences are not terrible. But in other cases, we may be assigning
111112

112113
What's worst -- usually developers do not think about such possibility at all. That makes such bugs hard to notice and even turn them into vulnerabilities, especially when JavaScript is used on server-side.
113114

114-
Unexpected things also may happen when accessing `toString` property -- that's a function by default, and other built-in properties.
115+
Unexpected things also may happen when assigning to `toString` -- that's a function by default, and other built-in methods.
115116

116117
How to evade the problem?
117118

@@ -160,7 +161,7 @@ alert(obj); // Error (no toString)
160161

161162
...But that's usually fine for associative arrays.
162163

163-
Please note that most object-related methods are `Object.something(...)`, like `Object.keys(obj)` -- they are not in the prototype, so they will keep working on such objects:
164+
Note that most object-related methods are `Object.something(...)`, like `Object.keys(obj)` -- they are not in the prototype, so they will keep working on such objects:
164165

165166

166167
```js run

‎1-js/09-classes/01-class/article.md‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class MyClass {
2525
}
2626
```
2727

28-
Then `new MyClass()` creates a new object with all the listed methods.
28+
Then use `new MyClass()` to create a new object with all the listed methods.
2929

3030
The `constructor()` method is called automatically by `new`, so we can initialize the object there.
3131

@@ -53,7 +53,7 @@ When `new User("John")` is called:
5353
1. A new object is created.
5454
2. The `constructor` runs with the given argument and assigns `this.name` to it.
5555

56-
...Then we can call methods, such as `user.sayHi`.
56+
...Then we can call object methods, such as `user.sayHi()`.
5757

5858

5959
```warn header="No comma between class methods"
@@ -191,7 +191,7 @@ let User = class {
191191
};
192192
```
193193

194-
Similar to Named Function Expressions, class expressions may or may not have a name.
194+
Similar to Named Function Expressions, class expressions may have a name.
195195

196196
If a class expression has a name, it's visible inside the class only:
197197
@@ -200,13 +200,13 @@ If a class expression has a name, it's visible inside the class only:
200200
// (no such term in the spec, but that's similar to Named Function Expression)
201201
let User = class *!*MyClass*/!* {
202202
sayHi() {
203-
alert(MyClass); // MyClass is visible only inside the class
203+
alert(MyClass); // MyClass name is visible only inside the class
204204
}
205205
};
206206

207207
new User().sayHi(); // works, shows MyClass definition
208208

209-
alert(MyClass); // error, MyClass not visible outside of the class
209+
alert(MyClass); // error, MyClass name isn't visible outside of the class
210210
```
211211

212212

@@ -282,13 +282,14 @@ Object.defineProperties(User.prototype, {
282282
});
283283
```
284284

285-
Here's an example with computed properties:
285+
Here's an example with a computed property in brackets `[...]`:
286286

287287
```js run
288-
function f() { return "sayHi"; }
289-
290288
class User {
291-
[f()]() {
289+
290+
*!*
291+
['say' + 'Hi']() {
292+
*/!*
292293
alert("Hello");
293294
}
294295

@@ -309,7 +310,9 @@ In the example above, `User` only had methods. Let's add a property:
309310

310311
```js run
311312
class User {
313+
*!*
312314
name = "Anonymous";
315+
*/!*
313316

314317
sayHi() {
315318
alert(`Hello, ${this.name}!`);
@@ -319,16 +322,15 @@ class User {
319322
new User().sayHi();
320323
```
321324

322-
The property is not placed into `User.prototype`. Instead, it is created by `new`, separately for every object. So, the property will never be shared between different objects of the same class.
323-
325+
The property `name` is not placed into `User.prototype`. Instead, it is created by `new` before calling constructor, it's the property of the object itself.
324326

325327
## Summary
326328

327329
The basic class syntax looks like this:
328330

329331
```js
330332
class MyClass {
331-
prop = value; // field
333+
prop = value; // property
332334

333335
constructor(...) { // constructor
334336
// ...
@@ -339,7 +341,7 @@ class MyClass {
339341
get something(...) {} // getter method
340342
set something(...) {} // setter method
341343

342-
[Symbol.iterator]() {} // method with computed name/symbol name
344+
[Symbol.iterator]() {} // method with computed name (symbol here)
343345
// ...
344346
}
345347
```

‎1-js/09-classes/02-class-inheritance/article.md‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Internally, `extends` keyword adds `[[Prototype]]` reference from `Rabbit.protot
9292

9393
So, if a method is not found in `Rabbit.prototype`, JavaScript takes it from `Animal.prototype`.
9494

95-
As we can recall from the chapter <info:native-prototypes>, JavaScript uses the same prototypal inheritance for build-in objects. E.g. `Date.prototype.[[Prototype]]` is `Object.prototype`, so dates have generic object methods.
95+
As we can recall from the chapter <info:native-prototypes>, JavaScript uses prototypal inheritance for build-in objects. E.g. `Date.prototype.[[Prototype]]` is `Object.prototype`, so dates have generic object methods.
9696

9797
````smart header="Any expression is allowed after `extends`"
9898
Class syntax allows to specify not just a class, but any expression after `extends`.
@@ -131,7 +131,6 @@ class Rabbit extends Animal {
131131
}
132132
```
133133
134-
135134
...But usually we don't want to totally replace a parent method, but rather to build on top of it, tweak or extend its functionality. We do something in our method, but call the parent method before/after it or in the process.
136135
137136
Classes provide `"super"` keyword for that.

‎1-js/09-classes/02-class-inheritance/rabbit-animal-independent-animal.svg‎

Lines changed: 2 additions & 2 deletions
Loading[フレーム]

‎1-js/09-classes/03-static-properties-methods/animal-rabbit-static.svg‎

Lines changed: 3 additions & 3 deletions
Loading[フレーム]

‎figures.sketch‎

-68 Bytes
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /