我如何做到这一点:
function Vehicle(){
this.mobility = true;
};
function Car(){};
Car.prototype = new Vehicle();
var myCar = new Car();
console.log(myCar.mobility);
使用对象文字创建的对象?
我知道 Object.create() 但有什么方法可以像
Car.prototype = new Vehicle();
实现那个?
最佳答案
下面是使用 __proto__
的方法:
var propertiesToInherit = { 'horsepower': 201, 'make': 'Acura' }
var myCar = {};
myCar.__proto__ = propertiesToInherit;
console.log(myCar.horsepower); // 201
console.log(myCar.make); // Acura
话虽如此,我会避免这样做。看起来是deprecated
关于javascript - 在 JavaScript 中使用对象字面量实现继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11480186/