Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Constructor function for base Vehicle
- function Vehicle(make, model) {
- this.make = make;
- this.model = model;
- }
- // Method for starting the vehicle
- Vehicle.prototype.start = function() {
- console.log(`Starting the ${this.make} ${this.model}`);
- };
- // Constructor function for Car inheriting from Vehicle
- function Car(make, model, numDoors) {
- // Call the parent constructor using call() or apply()
- Vehicle.call(this, make, model);
- this.numDoors = numDoors;
- }
- // Inherit Vehicle prototype methods
- Car.prototype = Object.create(Vehicle.prototype);
- Car.prototype.constructor = Car;
- // Override start method for Car
- Car.prototype.start = function() {
- console.log(`Starting the ${this.make} ${this.model} with ${this.numDoors} doors`);
- };
- // Test the classes
- const myVehicle = new Vehicle('Toyota', 'Camry');
- myVehicle.start(); // Output: Starting the Toyota Camry
- const myCar = new Car('Honda', 'Civic', 4);
- myCar.start(); // Output: Starting the Honda Civic with 4 doors
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement