简体   繁体   中英

what is the reason for name begin undefined

I have a class vehicle and car and motorcycle every output is correct except for the issue is within the class vehicle it tells me that the name and wheel are declared but never used

 class Vehicle { constructor(name, wheels) { this.name; this.wheels; } drive() { return 'Moving Forward'; } stop() { return 'Stopping'; } } class Car extends Vehicle { constructor(name, wheels) { super(name, wheels); } } class Motorcycle extends Vehicle { constructor(name, wheels) { super(name, wheels); } wheelie() { return 'Wheee!'; } } console.log(''); console.log('ES6 CLASSES'); console.log(''); let mazda = new Car('Mazda 3'); console.log('Car'); console.log('mazda.name', mazda.name); console.log('mazda.drive()', mazda.drive()); console.log('mazda.stop()', mazda.stop()); console.log(mazda); console.log('-------------------------------'); let harley = new Motorcycle('Fatboy'); console.log('Motorcycle'); console.log('harley.name', harley.name); console.log('harley.drive()', harley.drive()); console.log('harley.wheelie()', harley.wheelie()); console.log('harley.stop()', harley.stop());
I have a class vehicle and car and motorcycle every output is correct except for the issue is within the class vehicle it tells me that the name and wheel are declared but never used

You're not assigning anything to this.name and this.wheels , so you're not using the arguments that you declared in the constructor of Vehicle .

Try this:

class Vehicle {
  constructor(name, wheels) {
    this.name = name;
    this.wheels = wheels;
  }
  …
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM