我有一个模块 Vehicle ,其中包含一般车辆信息。我有另一个模块 Car,它为 Vehicle 对象添加了更多功能。
// Pseudo code only. The final functions do not have to resemble this
var vehicle = require('vehicle')
vehicle.terrain = 'Land'
var car = vehicle.createCar()
// car and anotherCar will have unique Car-related values,
// but will use the same Vehicle info
var anotherCar = vehicle.createCar()
我正在考虑将 Object.create 用于 Car 模块,但不确定 Object.create 调用应该去哪里。
- 我是否应该在 Car 模块中有一个构造函数,该构造函数采用 Vehicle 对象的实例,并使用 Vehicle 实例作为原型进行 Object.create ?
- 或者 Object.create 是否应该发生在 Vehicle 对象的函数中,例如 createCar?我对这种方式的问题是,Car 应该关心它是从 Vehicle 派生的,Vehicle 不应该知道 Car 需要它。
- 或者即使 Object.create 是正确的方法。
请,任何示例和最佳实践将不胜感激。
更新:
我更改了示例以更好地反映我正在尝试解决的继承问题。