Advertisement
Guest User

Untitled

a guest
Jul 5th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. function Sale(price) {
  2. this.price = price || 100;
  3. this.decorators_list = [];
  4. }
  5. Sale.prototype.getPrice = function () {
  6. var price = this.price,
  7. i,
  8. max = this.decorators_list.length,
  9. name;
  10. for (i = 0; i < max; i+= 1) {
  11. name = this.decorators_list[i];
  12. price = Sale.decorators[name].getPrice(price);
  13. }
  14. return price;
  15. };
  16.  
  17. Sale.decorators = {}; // 装饰器
  18.  
  19. Sale.decorators.fedtax = {
  20. getPrice: function (price) {
  21. return price + price * 5 / 100;
  22. }
  23. };
  24.  
  25. Sale.decorators.quebec = {
  26. getPrice: function (price) {
  27. return price + price * 7.5 / 100;
  28. }
  29. };
  30.  
  31. Sale.prototype.decorate = function (decorator) {
  32. this.decorators_list.push(decorator);
  33. };
  34.  
  35. var sale = new Sale();
  36. console.log(sale.getPrice()); //100
  37. sale.decorate('fedtax');
  38. console.log('after fedtax:' + sale.getPrice()); //after fedtax:105
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement