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
A lot of details. But we can use it without knowing anything.
22
+
多くの構成要素があります。しかし、何も知らなくても私たちは使うことができます。
23
23
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.
25
24
26
-
The secret of reliability and simplicity of a coffee machine -- all details are well-tuned and *hidden* inside.
25
+
コーヒーメーカーはとても信頼性が高いですね。何年も使え、調子が悪い場合にだけ修正に持っていきます。
27
26
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).
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.
In JavaScript, there are three types of properties and members:
50
+
ここまでは一般的な前置きでした。
52
51
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種類のプロパティとメンバがあります。
55
53
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.
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).
**Protected properties are usually prefixed with an underscore `_`.**
89
+
**protected プロパティは、通常アンダースコア `_` で始まります。**
89
90
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.
That's exactly the case for a coffee machine: power never changes.
127
+
これはまさにコーヒーメーカーの電力(power)のケースです。この値は決して変わりません。
127
128
128
-
To do so, we only need to make getter, but not the setter:
129
+
そうするためには、getter のみを作成する必要があります。setter は不要です。:
129
130
130
131
```js run
131
132
classCoffeeMachine {
@@ -141,18 +142,18 @@ class CoffeeMachine {
141
142
142
143
}
143
144
144
-
//create the coffee machine
145
+
//コーヒーメーカーを作成
145
146
let coffeeMachine =newCoffeeMachine(100);
146
147
147
148
alert(`Power is: ${coffeeMachine.power}W`); // Power is: 100W
148
149
149
-
coffeeMachine.power=25; // Error (no setter)
150
+
coffeeMachine.power=25; // Error (setter はないので)
150
151
```
151
152
152
-
````smart header="Getter/setter functions"
153
-
Here we used getter/setter syntax.
153
+
````smart header="Getter/setter 関数"
154
+
ここでは、getter/setter 構文を使いました。
154
155
155
-
But most of the time `get.../set...` functions are preferred, like this:
156
+
しかし、多くの場合は次のような `get.../set...` 関数が好まれます。:
156
157
157
158
```js
158
159
class CoffeeMachine {
@@ -171,26 +172,26 @@ class CoffeeMachine {
171
172
new CoffeeMachine().setWaterAmount(100);
172
173
```
173
174
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.
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.
0 commit comments