Code is from mozilla website
function Product(name, price) {
this.name = name;
this.price = price;
if (price < 0)
throw RangeError('Cannot create product "' + name + '" with a negative price');
return this;
}
function Food(name, price) {
Product.call(this, name, price);/// why not this be Product(name,price)
this.category = 'food';
}
Food.prototype = new Product();
may very silly thing, can’t understand this line
Product.call(this, name, price);
Since both Product and Food are global functions why do we have to use Product.call
Because you want to apply the
Productfunction on the food object. Just invoking it withoutcallwould lead to a wrongthisvalue, and the properties added inProductwould be attached to the wrong object.Also learn here about inheritance.