File tree Expand file tree Collapse file tree 1 file changed +16
-20
lines changed
docs/2-browser-apps/03-class Expand file tree Collapse file tree 1 file changed +16
-20
lines changed Original file line number Diff line number Diff line change @@ -64,8 +64,24 @@ document.write(emptyFunction()); // 値を返さない関数の戻り値は unde
6464
6565` weightInTons ` と ` cost ` をプロパティとして持ち、 ` weightInTons ` のデフォルト値が ` 1 ` であるクラス ` Car ` を作成し、 ` cost ` に好きな値を代入してみましょう。
6666
67+ <Answer title = " Carクラスの定義" >
68+
69+ ``` javascript
70+ class Car {
71+ weightInTons = 1 ;
72+ cost;
73+ }
74+
75+ const prius = new Car ();
76+ prius .cost = 2600000 ;
77+
78+ document .write (` 重さは${ prius .weightInTons } tで、値段は${ prius .cost } 円です。` );
79+ ```
80+
6781<ViewSource url = { import .meta .url } path = " _samples/car-class" />
6882
83+ </Answer >
84+
6985## <Term >メソッド</Term >
7086
7187同じ<Term >プロパティ</Term >を持つ<Term >オブジェクト</Term >に対しては、同じような処理を行うことが多いです。例えば、学生はたいてい最初の授業で自己紹介をします。そこで、 ` Student ` <Term >クラス</Term >に、自己紹介をする関数 ` introduceSelf ` を設定してみましょう。
@@ -338,26 +354,6 @@ document.write(myBirthDay.getFullYear()); // 2014
338354
339355` getFullYear ` <Term >メソッド</Term >は、年となる数値を返す<Term >メソッド</Term >です。
340356
341- { /* prettier-ignore */ }
342- { /* TODO: 自分はこっちのほうがいいと思いますが...
343- 例えば [`Map` クラス](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Map)は、キーと値のペアを保存するオブジェクトを提供します。
344-
345- ```js
346- const map1 = new Map();
347-
348- map1.set("a", 1);
349- map1.set("b", 2);
350- map1.set("c", 3);
351-
352- document.write(map1.get("a")); // 1
353-
354- map1.set("a", 97);
355-
356- document.write(map1.get("a")); // 97
357- ```
358-
359- \*/ }
360-
361357また、DOM を利用して ` div ` 要素を作成または取得すると、[ ` HTMLDivElement ` クラス] ( https://developer.mozilla.org/ja/docs/Web/API/HTMLDivElement ) のインスタンスが得られます。
362358
363359このクラスは [ ` HTMLElement ` クラス] ( https://developer.mozilla.org/ja/docs/Web/API/HTMLElement ) を継承しており、 ` HTMLElement ` クラスは [ ` Element ` クラス] ( https://developer.mozilla.org/ja/docs/Web/API/Element ) を、` Element ` クラスは [ ` Node ` クラス] ( https://developer.mozilla.org/ja/docs/Web/API/Node ) を継承しています。
You can’t perform that action at this time.
0 commit comments