以下のコードを参照してください。 01 class Ship { 02 constructor(size) { 03 this.size = size; 04 } 05 } 06 07 class FishingBoat extends Ship { 08 constructor(size, capacity){ 09 //Missing code 10 this.capacity = capacity; 11 } 12 displayCapacity() { 13 console.log( ' The boat has a capacity of ${this.capacity} people. ' ); 14 } 15 } 16 17 let myBoat = new FishingBoat( ' medium ' , 10); 18 myBoat.displayCapacity(); コードに表示させるには、9行目にどのステートメントを追加すればよいでしょうか? そのボートの定員は10人ですか?
正解:A
FishingBoat extends Ship, so it is a subclass. In ES6 classes: * When you define a constructor in a subclass, you must call super(...) before accessing this. * super(size) calls the parent class (Ship) constructor, which sets this.size = size. So the correct constructor is: class FishingBoat extends Ship { constructor(size, capacity) { super(size); // line 09 this.capacity = capacity; } displayCapacity() { console.log(`The boat has a capacity of ${this.capacity} people.`); } } Why others are incorrect: * B. ship.size = size; * ship is not defined; this would cause a ReferenceError. * C. super.size = size; * super is not an instance; you must call super(...) as a function to invoke the parent constructor. * D. this.size = size; * In a subclass constructor, you must call super() before using this, otherwise you get a ReferenceError. Also this bypasses the parent constructor logic. Relevant concepts: ES6 class inheritance, extends, super() in subclass constructors, this initialization rules.