0

Assuming I had an ecmascript 5 class

function vehicle(){
    this.hasWheels=true;
} 

vehicle.prototype.getWheels=function(){return this.haswheels;};

but it was defined using the new ecmascript class syntax, would creating a "car" class that extends vehicle use vehicle.prototype as a prototype or would it use a new instance of vehicle?

Bergi
  • 513,640
  • 108
  • 821
  • 1,164
Jim Jones
  • 2,097
  • 5
  • 19
  • 41

1 Answers1

0

would creating a "car" class that extends vehicle use vehicle.prototype as a prototype or would it use a new instance of vehicle?

Neither. It does not simply extend not the vehicle.prototype, and it does not create an instance (new vehicle).

Instead, it properly creates a new object that inherits from the vehicle prototype, like

Car.prototype = Object.create(Vehicle.prototype);

However, it actually does more than that. In the maximally minimal classes proposal the extendsis defined in terms of the <| prototype operator (archived):

class Car extends Vehicle { constructor(){} }

is equivalent to

const Car = Vehicle <| function Car(){};

which in ES5 looks like

function Car(){}
Car.__proto__ = Vehicle; // not so much ES5
Car.prototype = Object.create(Vehicle.prototype);
Bergi
  • 513,640
  • 108
  • 821
  • 1,164