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?
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 extends
is 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);