【发布时间】:2017-09-11 19:32:10
【问题描述】:
这是进行原型继承和属性“窃取??”/继承的正确方法。 我想从 Person 构造函数中继承所有属性 + 所有方法。
function Product(name, price) {
this.name = name;
this.price = price;
}
Product.prototype.tellMe = function() {
console.log('Name = ' + this.name + ', Price = ' + this.price);
}
function Food(name, price, category) {
Product.call(this, name, price);
this.category = category;
}
Food.prototype = Object.create(Product.prototype);
Food.prototype.constructor = Food;
var b = new Food('Name', 123, 'Category');
console.log(b);
b.tellMe();
var a = new Product('Name2', 321);
console.log(a);
如果你能给我一个很好的例子,我将不胜感激。 谢谢!
【问题讨论】:
标签: javascript oop