Here is my code:
function Product(name, price) {
this.name = name;
this.price = price;
if (price < 0) throw RangeError('Invalid');
return this;
}
function Food(name, price) {
Product.call(this, name, price);
this.category = 'food';
}
Food.prototype = Object.create(Product.prototype);
var cheese = new Food('feta', 5);
When I check the variable in my console, I see the following:
Food {name: "feta", price: 5, category: "food"}
Which is what I had expected.
However, if I omit Object.create(Product.prototype)
I see the same results because of Product.call
.
Object.create(Product.prototype)
even necessary for inheritance and if so, why?