Skip to content

Commit be88cc4

Browse files
chvmvdkiyokaanan
andauthored
継承の演習問題にコンストラクタを追加 (#744)
Co-authored-by: kiyokaanan <[email protected]>
1 parent 54efbf4 commit be88cc4

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

docs/2-browser-apps/03-class/index.mdx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,22 +283,29 @@ class Student {
283283
name;
284284
age;
285285

286+
constructor(name, age) {
287+
this.name = name;
288+
this.age = age;
289+
}
290+
286291
introduceSelf() {
287292
document.write(`私の名前は${this.name}です。${this.age}歳です。`);
288293
}
289294
}
290295
class SeniorStudent extends Student {
291296
researchQuestion;
292297

298+
constructor(name, age, researchQuestion) {
299+
super(name, age);
300+
this.researchQuestion = researchQuestion;
301+
}
302+
293303
introduceSelf() {
294304
super.introduceSelf();
295305
document.write(`研究テーマは${this.researchQuestion}です。`);
296306
}
297307
}
298-
const tanaka = new SeniorStudent();
299-
tanaka.age = 22;
300-
tanaka.name = "田中";
301-
tanaka.researchQuestion = "量子力学";
308+
const tanaka = new SeniorStudent("田中", 22, "量子力学");
302309
tanaka.introduceSelf();
303310
```
304311

@@ -365,7 +372,7 @@ document.write({ name: "田中" }.toString()); // [object Object]
365372
// Date クラスは toString メソッドをオーバーライドしている
366373
document.write(new Date().toString()); // Fri Apr 01 2022 10:00:00 GMT+0900 (Japan Standard Time)
367374

368-
// 関数もオブジェクトの一種なのでやはり Object クラスを継承している
375+
// 関数もオブジェクトの一種なのでやはり Object クラスを継承し、toString メソッドをオーバーライドしている
369376
function add(a, b) {
370377
return a + b;
371378
}

0 commit comments

Comments
 (0)