File tree Expand file tree Collapse file tree 1 file changed +12
-5
lines changed
docs/2-browser-apps/03-class Expand file tree Collapse file tree 1 file changed +12
-5
lines changed Original file line number Diff line number Diff 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}
290295class 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 , " 量子力学" );
302309tanaka .introduceSelf ();
303310```
304311
@@ -365,7 +372,7 @@ document.write({ name: "田中" }.toString()); // [object Object]
365372// Date クラスは toString メソッドをオーバーライドしている
366373document .write (new Date ().toString ()); // Fri Apr 01 2022 10:00:00 GMT+0900 (Japan Standard Time)
367374
368- // 関数もオブジェクトの一種なのでやはり Object クラスを継承している
375+ // 関数もオブジェクトの一種なのでやはり Object クラスを継承し、toString メソッドをオーバーライドしている
369376function add (a , b ) {
370377 return a + b;
371378}
You can’t perform that action at this time.
0 commit comments