Skip to content

Instantly share code, notes, and snippets.

@justyn-clark
Created November 14, 2018 01:57
Show Gist options
  • Save justyn-clark/8a9063342b49c66361d6424f47322536 to your computer and use it in GitHub Desktop.
Save justyn-clark/8a9063342b49c66361d6424f47322536 to your computer and use it in GitHub Desktop.
function Sandwich(name) {
this.name = name;
}
Sandwich.prototype.eat = function () {
console.log("I'm eating " + this.name);
}
function Subway () {
Sandwich.call(this, 'subway');
}
Subway.prototype = Object.create(Sandwich.prototype);
Subway.prototype.cost = function () {
return 5;
}
////
Subway.sixInch = function (subwayObj) { //Method on Subway function object
var cost = subwayObj.cost(); //Get current cost of sandwich
subwayObj.cost = function () { //We aren't changing interface but providing more functionality
return cost - 1;
}
}
Subway.footLong = function (subwayObj) {}; //Default is Foot Long
Subway.partySub = function (subwayObj) {
var cost = subwayObj.cost();
subwayObj.cost = function () {
return cost + 3;
}
}
Subway.partySub = function (subwayObj) {
var cost = subwayObj.cost();
subwayObj.cost = function () {
return cost + 3;
}
}
Subway.pickle = function (subwayObj) {
var cost = subwayObj.cost();
subwayObj.cost = function () {
return cost + 0.12;
}
}
Subway.mustard = function (subwayObj) {
var cost = subwayObj.cost();
subwayObj.cost = function () {
return cost + 0.25;
}
}
Subway.ketchup = function (subwayObj) {
var cost = subwayObj.cost();
subwayObj.cost = function () {
return cost + 0.10;
}
}
var mySandwich = new Subway(); // current cost of sandwich 5
Subway.partySub(mySandwich); // add 3 and cost would be 8
Subway.pickle(mySandwich); // add 0.12 and cost would be 8.12
Subway.mustard(mySandwich); // add 0.25 and cost would be 8.37
console.log(mySandwich.cost());//8.37
function Sandwich() {
this._cost = 0;
}
Sandwich.prototype.cost = function () {
return this._cost;
}
function SandwichDecorator(sandwich) {
Sandwich.call(this);
this.sandwich = sandwich;
}
SandwichDecorator.prototype = Object.create(Sandwich.prototype);
SandwichDecorator.prototype.cost = function () {
return this._cost + this.sandwich.cost();
}
function Pickle(sandwich) {
SandwichDecorator.call(this, sandwich);
this._cost = 0.12;
}
Pickle.prototype = Object.create(SandwichDecorator.prototype);
function Subway() {
Sandwich.call(this);
this._cost = 5;
}
Subway.prototype = Object.create(Sandwich.prototype);
var mySandwich = new Subway();
mySandwich = new Pickle(mySandwich);
console.log(mySandwich.cost()); //5.12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment