JSFiddle - React, Tailwind, and code Playground
by Shawn
JavaScript
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;
}
Product.prototype.getDescription = function () {
return this.category + " named " + this.name;
};
function Food(name, price) {
Product.apply(this, arguments);
this.category = 'food';
}
Food.prototype = new Product(); // Food now inherits the getDescription method from Product's prototype
function Toy(name, price) {
Product.apply(this, arguments);
this.category = 'toy';
}
var cheese = new Food('feta', 5);
var fun = new Toy('robot', 40);
console.log(cheese.getDescription());
console.log(fun.getDescription());