JSFiddle - React, Tailwind, and code Playground
JavaScript
"use strict";
function Hamburger(size) {
try {
if (!this.validateInput(size, this.sizesAllowed)) {
throw new Error("Введены некорректные данные");
}
} catch (e) {
alert("Гамбургер может быть только размеров 'big' и 'small'!");
return;
}
this.size = size;
this.setBasicPrice();
this.setBasicCals();
this.spice = false;
this.mayonnaise = false;
}
Object.defineProperty(Hamburger.prototype, "sizesAllowed", {
value: ["small", "big"],
configurable: false,
writable: false
});
Hamburger.prototype.addSpice = function() {
this.spice = true;
this.price += 15;
};
Hamburger.prototype.addMayonnaise = function() {
this.mayonnaise = true;
this.price += 20;
this.cal += 5;
};
Hamburger.prototype.setBasicPrice = function() {
switch (this.size) {
case "small":
this.price = 50;
break;
case "big":
this.price = 100;
break;
}
};
Hamburger.prototype.setBasicCals = function() {
switch (this.size) {
case "small":
this.cal = 20;
break;
case "big":
this.cal = 40;
break;
}
};
Hamburger.prototype.validateInput = function(value, valuesAllowed) {
var result = false;
valuesAllowed.forEach(function(item, i, valuesAllowed) {
if (item === value) {
result = true;
return;
}
});
return result;
};
function HamburgerWithCheese() {
Hamburger.apply(this, arguments);
this.stuffing = "cheese";
this.cal += 20;
this.price += 10;
}
HamburgerWithCheese.prototype = Object.create(Hamburger.prototype);
function HamburgerWithSalad() {
Hamburger.apply(this, arguments);
this.stuffing = "salad";
this.cal += 5;
this.price += 20;
}
HamburgerWithSalad.prototype = Object.create(Hamburger.prototype);
function HamburgerWithPotatoe() {
Hamburger.apply(this, arguments);
...