Skip to content

Commit fad7c28

Browse files
committed
WIP private-protected-properties-methods
1 parent 40afb56 commit fad7c28

1 file changed

Lines changed: 68 additions & 66 deletions

File tree

  • 1-js/09-classes/05-private-protected-properties-methods

1-js/09-classes/05-private-protected-properties-methods/article.md

Lines changed: 68 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,72 @@
11

2-
# Private and protected properties and methods
2+
# Private / protected プロパティとメソッド
33

4-
One of the most important principles of object oriented programming -- delimiting internal interface from the external one.
4+
オブジェクト指向プログラミングの最も重要な原則の1つは、内部インタフェースを外部インタフェースから切り離すことです。
55

6-
That is "a must" practice in developing anything more complex than a "hello world" app.
6+
これは、"hellow world" アプリケーションよりも複雑なものを作るすべての開発において "必須" です。
77

8-
To understand this, let's break away from development and turn our eyes into the real world.
8+
これを理解するために、開発から離れて、現実の正解に目を向けましょう。
99

10-
Usually, devices that we're using are quite complex. But delimiting the internal interface from the external one allows to use them without problems.
10+
通常、私たちが使っているデバイスは非常に複雑です。しかし、内部インタフェースを外部インタフェースから切り離すことで、問題なく使うことができます。
1111

12-
## A real-life example
12+
## 実世界の例
1313

14-
For instance, a coffee machine. Simple from outside: a button, a display, a few holes...And, surely, the result -- great coffee! :)
14+
例えばコーヒーメーカーです。外側はシンプルです: ボタン、ディスプレイやいくつかの穴があるだけです。そして、もちろん結果はおいしいコーヒーです :)
1515

1616
![](coffee.jpg)
1717

18-
But inside... (a picture from the repair manual)
18+
しかし内側は...(修理マニュアルにある図です)
1919

2020
![](coffee-inside.jpg)
2121

22-
A lot of details. But we can use it without knowing anything.
22+
多くの構成要素があります。しかし、何も知らなくても私たちは使うことができます。
2323

24-
Coffee machines are quite reliable, aren't they? We can use one for years, and only if something goes wrong -- bring it for repairs.
2524

26-
The secret of reliability and simplicity of a coffee machine -- all details are well-tuned and *hidden* inside.
25+
コーヒーメーカーはとても信頼性が高いですね。何年も使え、調子が悪い場合にだけ修正に持っていきます。
2726

28-
If we remove the protective cover from the coffee machine, then using it will be much more complex (where to press?), and dangerous (it can electrocute).
27+
コーヒーメーカーの信頼性とシンプルさの秘密は、すべての構成要素がよく調整され、内部に *隠れている* ことです。
2928

30-
As we'll see, in programming objects are like coffee machines.
29+
もしコーヒーメーカーの保護カバーを外すと、使うのが非常に複雑になり(どこを押せばよい?)、危険です(感電するかもしれません)。
3130

32-
But in order to hide inner details, we'll use not a protective cover, but rather special syntax of the language and conventions.
31+
これから見ていきますが、プログラミングにおいてオブジェクトはコーヒーメーカーのようなものです。
3332

34-
## Internal and external interface
33+
しかし、内部の詳細を隠すには、保護カバーではなく言語や規則の特別な構文を使っていきます。
3534

36-
In object-oriented programming, properties and methods are split into two groups:
35+
## 内部 / 外部インタフェース
3736

38-
- *Internal interface* -- methods and properties, accessible from other methods of the class, but not from the outside.
39-
- *External interface* -- methods and properties, accessible also from outside the class.
37+
オブジェクト指向プログラミングでは、プロパティとメソッドは2つのグループに分けられます:
4038

41-
If we continue the analogy with the coffee machine -- what's hidden inside: a boiler tube, heating element, and so on -- is its internal interface.
39+
- *内部インタフェース*: クラスの他のメソッドからアクセス可能だが、外側からはアクセスできないメソッドやプロパティ。
40+
- *外部インタフェース*: 外部のクラスからもアクセス可能なメソッドやプロパティ。
4241

43-
An internal interface is used for the object to work, its details use each other. For instance, a boiler tube is attached to the heating element.
42+
コーヒーメーカーで例えるなら、内部に隠されているもの: ボイラーチューブや発熱体など、は内部インタフェースです。
4443

45-
But from the outside a coffee machine is closed by the protective cover, so that no one can reach those. Details are hidden and inaccessible. We can use its features via the external interface.
44+
内部インタフェースはオブジェクトが機能するために使われ、その構成要素はお互いに使用されます。例えば、ボイラーチューブは発熱体に取り付けられます。
4645

47-
So, all we need to use an object is to know its external interface. We may be completely unaware how it works inside, and that's great.
46+
しかし、コーヒーメーカーの外側からは、誰もそこに届かないよう保護カバーで閉ざされています。内部の詳細は隠されており、アクセスできません。私たちは、外部インタフェースを介してのみその機能を利用できます。
4847

49-
That was a general introduction.
48+
したがって、オブジェクトを使用するのに必要なことは、その外部インタフェースを知ることです。内部でどのように動いているか完全に分からないかもしれませんが、問題ありません。
5049

51-
In JavaScript, there are three types of properties and members:
50+
ここまでは一般的な前置きでした。
5251

53-
- Public: accessible from anywhere. They comprise the external interface. Till now we were only using public properties and methods.
54-
- Private: accessible only from inside the class. These are for the internal interface.
52+
JavaScript には、3種類のプロパティとメンバがあります。
5553

56-
In many other languages there also exist "protected" fields: accessible only from inside the class and those extending it. They are also useful for the internal interface. They are in a sense more widespread than private ones, because we usually want inheriting classes to gain access to properly do the extension.
54+
- パブリック(public): どこからでもアクセス可能です。これらは外部インタフェースになります。今まで、私たちはパブリックなプロパティとメソッドのみを使用していました。
55+
- プライベート(private): クラス内部からのみアクセスできます。これらは内部インタフェース用です。
5756

58-
Protected fields are not implemented in Javascript on the language level, but in practice they are very convenient, so they are emulated.
57+
他の多くの言語には、"プロテクト(protected)" フィールドも存在します。: これは、クラス及び、そのクラスを継承したサブクラスの内部からのみアクセス可能であることを意味します。これも内部インタフェースには役立ちます。通常は、継承しているクラスを適切に拡張できるよう、それらにアクセスさせたいため、ある意味ではプライベートよりも広く知られています。
5958

60-
In the next step we'll make a coffee machine in Javascript with all these types of properties. A coffee machine has a lot of details, we won't model them to stay simple (though we could).
59+
protected フィールドは言語レベルでは Javascript に実装されていません。が、実際には非常に便利であるため、エミュレートされています。
6160

62-
## Protecting "waterAmount"
61+
次のステップでは、これらすべての種類のプロパティを使用した JavaScript でコーヒーメーカーを作ります。コーヒーメーカーには多くの構成要素がありますが、シンプルさを保つためにモデル化はしません(モデル化することも可能です)。
6362

64-
Let's make a simple coffee machine class first:
63+
## "waterAmount" を保護(protect)する
64+
65+
最初に、単純なコーヒーメーカークラスを作りましょう。:
6566

6667
```js run
6768
class CoffeeMachine {
68-
waterAmount = 0; // the amount of water inside
69+
waterAmount = 0; // 内部の水の量
6970

7071
constructor(power) {
7172
this.power = power;
@@ -74,22 +75,22 @@ class CoffeeMachine {
7475

7576
}
7677

77-
// create the coffee machine
78+
// コーヒーメーカーを生成
7879
let coffeeMachine = new CoffeeMachine(100);
7980

80-
// add water
81+
// 水を追加
8182
coffeeMachine.waterAmount = 200;
8283
```
8384

84-
Right now the properties `waterAmount` and `power` are public. We can easily get/set them from the outside to any value.
85+
今のところ、プロパティ `waterAmount` `power` public です。外側から簡単に値を取得したり、任意の値に設定できます。
8586

86-
Let's change `waterAmount` property to protected to have more control over it. For instance, we don't want anyone to set it below zero.
87+
より細かく制御できるように、`waterAmount` プロパティを protected に変更しましょう。例えば、誰もゼロより小さくは設定できないようにしたいです。
8788

88-
**Protected properties are usually prefixed with an underscore `_`.**
89+
**protected プロパティは、通常アンダースコア `_` で始まります。**
8990

90-
That is not enforced on the language level, but there's a convention that such properties and methods should not be accessed from the outside. Most programmers follow it.
91+
これは言語レベルでは強制されていませんが、このようなプロパティやメソッドは外側からアクセスするべきではない、という慣習があります。ほとんどのプログラマはそれに従っています。
9192

92-
So our property will be called `_waterAmount`:
93+
なので、プロパティは `_waterAmount` になります:
9394

9495
```js run
9596
class CoffeeMachine {
@@ -101,7 +102,7 @@ class CoffeeMachine {
101102
}
102103

103104
get waterAmount() {
104-
return this.waterAmount;
105+
return this._waterAmount;
105106
}
106107

107108
constructor(power) {
@@ -110,22 +111,22 @@ class CoffeeMachine {
110111

111112
}
112113

113-
// create the coffee machine
114+
// コーヒーメーカーを生成
114115
let coffeeMachine = new CoffeeMachine(100);
115116

116-
// add water
117+
// 水を追加
117118
coffeeMachine.waterAmount = -10; // Error: Negative water
118119
```
119120

120-
Now the access is under control, so setting the water below zero fails.
121+
これでアクセスが制御されたので、ゼロより小さい値へ設定しようとしても失敗します。
121122

122-
## Read-only "power"
123+
## 読み取り専用(Read-only)の "power"
123124

124-
For `power` property, let's make it read-only. It sometimes happens that a property must be set at creation time only, and then never modified.
125+
`power` プロパティは、読み取り専用にしましょう。作成時にのみ設定し、それ以降変更しないプロパティも時にあります。
125126

126-
That's exactly the case for a coffee machine: power never changes.
127+
これはまさにコーヒーメーカーの電力(power)のケースです。この値は決して変わりません。
127128

128-
To do so, we only need to make getter, but not the setter:
129+
そうするためには、getter のみを作成する必要があります。setter は不要です。:
129130

130131
```js run
131132
class CoffeeMachine {
@@ -141,18 +142,18 @@ class CoffeeMachine {
141142

142143
}
143144

144-
// create the coffee machine
145+
// コーヒーメーカーを作成
145146
let coffeeMachine = new CoffeeMachine(100);
146147

147148
alert(`Power is: ${coffeeMachine.power}W`); // Power is: 100W
148149

149-
coffeeMachine.power = 25; // Error (no setter)
150+
coffeeMachine.power = 25; // Error (setter はないので)
150151
```
151152

152-
````smart header="Getter/setter functions"
153-
Here we used getter/setter syntax.
153+
````smart header="Getter/setter 関数"
154+
ここでは、getter/setter 構文を使いました。
154155
155-
But most of the time `get.../set...` functions are preferred, like this:
156+
しかし、多くの場合は次のような `get.../set...` 関数が好まれます。:
156157
157158
```js
158159
class CoffeeMachine {
@@ -171,26 +172,26 @@ class CoffeeMachine {
171172
new CoffeeMachine().setWaterAmount(100);
172173
```
173174
174-
That looks a bit longer, but functions are more flexible. They can accept multiple arguments (even if we don't need them right now). So, for the future, just in case we need to refactor something, functions are a safer choise.
175+
これは少し長く見えますが、関数はより柔軟です。たとえいま時点では必要ないとしても、この方法の場合、複数の引数を受け取ることができます。そのため、将来なにかをリファクタする必要がある場合に備えるなら、関数はより安全な選択肢です。
175176
176-
Surely, there's a tradeoff. On the other hand, get/set syntax is shorter, so ultimately there's no strict rule, it's up to you to decide.
177+
もちろん、これはトレードオフです。一方で get/set 構文はより短くかけます。ここに厳密なルールはないので、決めるのはあなた次第です。
177178
````
178179

179-
```smart header="Protected fields are inherited"
180-
If we inherit `class MegaMachine extends CoffeeMachine`, then nothing prevents us from accessing `this._waterAmount` or `this._power` from the methods of the new class.
180+
```smart header="Protected フィールドは継承されます"
181+
`class MegaMachine extends CoffeeMachine` と継承した場合、新しいクラスのメソッドから `this._waterAmount` `this._power` にアクセスするのを妨げるものは何もありません。
181182
182-
So protected fields are naturally inheritable. Unlike private ones that we'll see below.
183+
つまり、protected フィールは当然のことながら継承可能です。下で見ていく private なものとは異なります。
183184
```
184185

185186
## Private "#waterLimit"
186187

187188
[recent browser=none]
188189

189-
There's a finished Javascript proposal, almost in the standard, that provides language-level support for private properties and methods.
190+
プライベートなプロパティやメソッドに対する言語レベルのサポートを提供する、ほぼ標準的な完成したJavascriptの提案があります。
190191

191-
Privates should start with `#`. They are only accessible from inside the class.
192+
プライベートは `#` から始める必要があります。それらはクラス内部からのみアクセス可能です。
192193

193-
For instance, here we add a private `#waterLimit` property and extract the water-checking logic into a separate method:
194+
例えば、ここではプライベートな `#waterLimit` プロパティを追加し、水量をチェックするロジックを別のメソッドに抜き出しています:
194195

195196
```js
196197
class CoffeeMachine {
@@ -230,11 +231,11 @@ coffeeMachine.#waterLimit = 1000; // Error
230231
coffeeMachine.waterAmount = 100; // Works
231232
```
232233

233-
On the language level, `#` is a special sign that the field is private. We can't access it from outside or from inhereting classes.
234+
言語レベルで、`#` はフィールドがプライベートであることを示す特別な記号です。その外側や継承したクラスからアクセスすることはできません。
234235

235-
Private fields do not conflict with public ones. We can have both private `#waterAmount` and public `waterAmount` fields at the same time.
236+
プライベートフィールドはパブリックなものと衝突しません。プライベートな `#waterAmount` とパブリックな `waterAmount` フィールド両方を同時にもつことができます。
236237

237-
For instance, let's make `waterAmount` an accessor for `#waterAmount`:
238+
例えば、`#waterAmount` のアクセサとなる `waterAmount` を作りましょう。:
238239

239240
```js run
240241
class CoffeeMachine {
@@ -257,15 +258,16 @@ machine.waterAmount = 100;
257258
alert(machine.#waterAmount); // Error
258259
```
259260

260-
Unlike protected ones, private fields are enforced by the language itselfs. That's a good thing.
261+
protected なものとは異なり、private フィールドは言語レベルで強制されます。これは良いことです。
261262

262-
But if we inherit from `CoffeeMachine`, then we'll have no direct access to `#waterAmount`. We'll need to rely on `waterAmount` getter/setter:
263+
しかし、`CoffeeMachine` を継承した場合、`#waterAmount` へアクセスはできません。`waterAmount` の getter/setter に頼る必要があります。
264+
:
263265

264266
```js
265267
class CoffeeMachine extends CoffeeMachine() {
266268
method() {
267269
*!*
268-
alert( this.#waterAmount ); // Error: can only access from CoffeeMachine
270+
alert( this.#waterAmount ); // Error: CoffeeMachine からのみアクセス可能
269271
*/!*
270272
}
271273
}

0 commit comments

Comments
 (0)