0

假设我有一个 ecmascript 5 类

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

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

extends但它是使用新的 ecmascript 类语法定义的,会创建一个车辆用作原型的“汽车”类,vehicle.prototype还是会使用新的车辆实例?

4

1 回答 1

0

创建一个扩展车辆的“汽车”类会使用vehicle.prototype作为原型还是使用新的车辆实例?

两者都不。它不会简单地扩展不vehicle.prototype,也不会创建实例 ( new vehicle)。

相反,它正确地创建了一个继承自车辆原型的新对象,例如

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

然而,它实际上做的远不止这些。在最大最小类提案extends中,根据<|原型运算符已归档)定义:

class Car extends Vehicle { constructor(){} }

相当于

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

在 ES5 中看起来像

function Car(){}
Car.__proto__ = Vehicle; // not so much ES5
Car.prototype = Object.create(Vehicle.prototype);
于 2014-04-27T17:17:42.647 回答