Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Add Comment
Please, Sign In to add comment