2
これは、プロトタイプの継承とプロパティ 'stealing ??' /継承を行う正しい方法ですか? Personコンストラクタとすべてのメソッドからすべてのプロパティを継承したいとします。JavaScriptプロトタイププロパティstealing/inheritance
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);
私には良い例がありますか? ありがとう!