Guest User

Untitled

a guest
Nov 14th, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. function Sandwich(name) {
  2. this.name = name;
  3. }
  4.  
  5. Sandwich.prototype.eat = function () {
  6. console.log("I'm eating " + this.name);
  7. }
  8.  
  9. function Subway () {
  10. Sandwich.call(this, 'subway');
  11. }
  12.  
  13. Subway.prototype = Object.create(Sandwich.prototype);
  14. Subway.prototype.cost = function () {
  15. return 5;
  16. }
  17.  
  18. ////
  19.  
  20. Subway.sixInch = function (subwayObj) { //Method on Subway function object
  21. var cost = subwayObj.cost(); //Get current cost of sandwich
  22. subwayObj.cost = function () { //We aren't changing interface but providing more functionality
  23. return cost - 1;
  24. }
  25. }
  26.  
  27. Subway.footLong = function (subwayObj) {}; //Default is Foot Long
  28. Subway.partySub = function (subwayObj) {
  29. var cost = subwayObj.cost();
  30. subwayObj.cost = function () {
  31. return cost + 3;
  32. }
  33. }
  34.  
  35. Subway.partySub = function (subwayObj) {
  36. var cost = subwayObj.cost();
  37. subwayObj.cost = function () {
  38. return cost + 3;
  39. }
  40. }
  41.  
  42. Subway.pickle = function (subwayObj) {
  43. var cost = subwayObj.cost();
  44. subwayObj.cost = function () {
  45. return cost + 0.12;
  46. }
  47. }
  48.  
  49. Subway.mustard = function (subwayObj) {
  50. var cost = subwayObj.cost();
  51. subwayObj.cost = function () {
  52. return cost + 0.25;
  53. }
  54. }
  55.  
  56. Subway.ketchup = function (subwayObj) {
  57. var cost = subwayObj.cost();
  58. subwayObj.cost = function () {
  59. return cost + 0.10;
  60. }
  61. }
  62.  
  63.  
  64. var mySandwich = new Subway(); // current cost of sandwich 5
  65. Subway.partySub(mySandwich); // add 3 and cost would be 8
  66. Subway.pickle(mySandwich); // add 0.12 and cost would be 8.12
  67. Subway.mustard(mySandwich); // add 0.25 and cost would be 8.37
  68. console.log(mySandwich.cost());//8.37
  69.  
  70.  
  71. function Sandwich() {
  72. this._cost = 0;
  73. }
  74.  
  75. Sandwich.prototype.cost = function () {
  76. return this._cost;
  77. }
  78.  
  79. function SandwichDecorator(sandwich) {
  80. Sandwich.call(this);
  81. this.sandwich = sandwich;
  82. }
  83. SandwichDecorator.prototype = Object.create(Sandwich.prototype);
  84. SandwichDecorator.prototype.cost = function () {
  85. return this._cost + this.sandwich.cost();
  86. }
  87.  
  88. function Pickle(sandwich) {
  89. SandwichDecorator.call(this, sandwich);
  90. this._cost = 0.12;
  91. }
  92. Pickle.prototype = Object.create(SandwichDecorator.prototype);
  93.  
  94. function Subway() {
  95. Sandwich.call(this);
  96. this._cost = 5;
  97. }
  98. Subway.prototype = Object.create(Sandwich.prototype);
  99.  
  100.  
  101. var mySandwich = new Subway();
  102. mySandwich = new Pickle(mySandwich);
  103. console.log(mySandwich.cost()); //5.12
Add Comment
Please, Sign In to add comment