-
-
Notifications
You must be signed in to change notification settings - Fork 228
fix: 「クラス」における誤字修正や説明の改善 #1598
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: 「クラス」における誤字修正や説明の改善 #1598
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -150,7 +150,6 @@ MyClass(); // => TypeError: class constructors must be invoked with |new| | |
| ``` | ||
|
|
||
| また、コンストラクタは初期化処理を書く場所であるため、`return`文で値を返すべきではありません。 | ||
| JavaScriptでは、コンストラクタ関数が任意のオブジェクトを返すことが可能ですが、行うべきではありません。 | ||
| なぜなら、コンストラクタは`new`演算子で呼び出し、その評価結果はクラスのインスタンスを期待するのが一般的であるためです。 | ||
|
|
||
| 次のコードのようにコンストラクタで返した値が`new`演算子で呼び出した際の返り値となります。 このような書き方は混乱を生むため避けるべきです。 | ||
|
|
@@ -458,8 +457,8 @@ class ArrayLike { | |
|
|
||
| set length(newLength) { | ||
| const currentItemLength = this.items.length; | ||
| // 現在要素数より小さな`newLength`が指定された場合、指定した要素数となるように末尾を削除する | ||
| if (newLength < currentItemLength) { | ||
| // 現在要素数より小さな`newLength`が指定された場合、指定した要素数となるように末尾を削除する | ||
|
||
| this._items = this.items.slice(0, newLength); | ||
| } else if (newLength > currentItemLength) { | ||
| // 現在要素数より大きな`newLength`が指定された場合、指定した要素数となるように末尾に空要素を追加する | ||
|
|
@@ -481,7 +480,7 @@ console.log(arrayLike.items.join(", ")); // => "1, 2, , , " | |
|
|
||
| ## [ES2022] Publicクラスフィールド {#public-class-fields} | ||
|
|
||
| クラスでは、`constructor`メソッドの中でクラスの状態であるインスタンスのプロパティの初期化することを紹介しました。 | ||
| クラスでは、`constructor`メソッドの中でクラスの状態であるインスタンスのプロパティを初期化することを紹介しました。 | ||
| 先ほども紹介した`Counter`クラスでは、`constructor`メソッドの中で`count`プロパティの初期値を`0`として定義しています。 | ||
|
|
||
| ```js | ||
|
|
@@ -535,7 +534,7 @@ console.log(counter.count); // => 1 | |
| {{book.console}} | ||
| <!-- doctest:meta:{ "ECMAScript": "2022" } --> | ||
| ```js | ||
| // 別々のプロパティ名はそれぞれ定義される | ||
| // 別々のプロパティ名がそれぞれ定義される | ||
| class MyClass { | ||
| publicField = 1; | ||
| constructor(arg) { | ||
|
|
@@ -637,7 +636,7 @@ console.log(counter.count); // => 1 | |
|
|
||
| クラスフィールドでの`this`は、Arrow Functionと組み合わせると強力です。 | ||
|
|
||
| 次のコードでは、`up`メソッドに`this.increment`メソッドを呼び出す関数をArrow Function定義しています。 | ||
| 次のコードでは、`up`メソッドを`this.increment`メソッドを呼び出すArrow Functionとして定義しています。 | ||
morinokami marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Arrow Functionで定義した関数における`this`は、どのような呼び出し方をしても変化しません(「[Arrow Functionでコールバック関数を扱う][]」を参照)。 | ||
| そのため、`up`メソッドはどのような呼び方をした場合でも`this`がクラスのインスタンスとなるため、確実に`increment`メソッドを呼び出せます。 | ||
|
|
||
|
|
@@ -723,7 +722,7 @@ setterは`=`での代入に反応します。そのため、`constructor`の中 | |
|
|
||
| ## [ES2022] Privateクラスフィールド {#private-class-fields} | ||
|
|
||
| クラスフィールド構文で次のように書くと、定義したプロパティはクラスのインスタンス化した後に外からも参照できます。 | ||
| クラスフィールド構文で次のように書くと、定義したプロパティはクラスをインスタンス化した後に外からも参照できます。 | ||
| そのため、Publicクラスフィールドと呼ばれます。 | ||
|
|
||
| ```js | ||
|
|
@@ -920,9 +919,9 @@ console.log(arrayWrapper.length); // => 3 | |
|
|
||
| ### [ES2022] 静的クラスフィールド {#static-class-fields} | ||
|
|
||
| ES2022で追加されたクラスフィールドも、インスタンスではなくクラス自体に定義する静的クラスフィールドが利用できます。 | ||
| ES2022で追加されたクラスフィールドでは、インスタンスではなくクラス自体に定義する静的クラスフィールドも利用できます。 | ||
|
|
||
| 静的クラスフィールドはフィールドの前に、`static`をつけるだけです。 | ||
| 静的クラスフィールドは、フィールドの前に`static`をつけるだけです。 | ||
| 静的クラスフィールドで定義したプロパティは、クラス自体のプロパティとして定義されます。 | ||
| 次のコードでは、Public静的クラスフィールドを使って`Colors`クラス自体にプロパティを定義しています。 | ||
|
|
||
|
|
@@ -986,7 +985,7 @@ example.instanceMethod(); | |
| example.prototypeMethod(); | ||
| ``` | ||
|
|
||
| しかしこの2つのメソッドの定義方法は、メソッドを定義先となるオブジェクトが実際に異なります。 | ||
| しかしこの2つのメソッドの定義方法は、メソッドの定義先となるオブジェクトが実際は異なります。 | ||
|
||
|
|
||
| まず、この2種類のメソッドがそれぞれ別の場所へと定義されていることを見ていきます。 | ||
| 次のコードでは、`ConflictClass`クラスに`method`という同じ名前のメソッドをプロトタイプメソッドとインスタンスに対してそれぞれ定義しています。 | ||
|
|
@@ -1395,7 +1394,7 @@ console.log(instance.field); // => "子クラスで定義したフィールド" | |
| Publicクラスフィールドは、このように親クラスで定義したフィールドも子クラスに定義されます。 | ||
| 一方で、Privateクラスフィールドは、このように親クラスで定義したフィールドは子クラスに定義されません。 | ||
|
|
||
| 次のコードでは、親クラスで定義したPrivateクラスフィールドを子クラスから参照してようとしています。 | ||
| 次のコードでは、親クラスで定義したPrivateクラスフィールドを子クラスから参照しようとしています。 | ||
| しかし、`#parentField`は参照できずに構文エラーとなることがわかります。 | ||
|
|
||
| <!-- textlint-disable eslint --> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この箇所について、完全に同じではありませんが似たようなことが続いて書かれており不自然に感じたため、後半の文を削除しました。
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
微妙に異なることを言っていて、どちらかという後者の方がストレートな表現な気がします。
前者は、仕様的にできないようにも読めます。コードの対応を考えると後者をベースにしたほうが良い気がしました