In Javascript, how do I create a constructor for another class that extends another…
- User
In Javascript, how do I create a constructor for another class that extends another class? Can you please show me an example?
Assistant
Here is an example of creating a constructor for a class that extends another class in Javascript:
js// Base class class Vehicle { constructor() { this.type = 'Vehicle'; } getType() { return this.type; } } // Class that extends Vehicle class Car extends Vehicle { constructor() { super(); this.type = 'Car'; this.wheels = 4; } getWheels() { return this.wheels; } } // Usage: let myCar = new Car(); console.log(myCar.getType()); // Prints "Car" console.log(myCar.getWheels()); // Prints 4
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.