Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Sale(price) {
- this.price = price || 100;
- this.decorators_list = [];
- }
- Sale.prototype.getPrice = function () {
- var price = this.price,
- i,
- max = this.decorators_list.length,
- name;
- for (i = 0; i < max; i+= 1) {
- name = this.decorators_list[i];
- price = Sale.decorators[name].getPrice(price);
- }
- return price;
- };
- Sale.decorators = {}; // 装饰器
- Sale.decorators.fedtax = {
- getPrice: function (price) {
- return price + price * 5 / 100;
- }
- };
- Sale.decorators.quebec = {
- getPrice: function (price) {
- return price + price * 7.5 / 100;
- }
- };
- Sale.prototype.decorate = function (decorator) {
- this.decorators_list.push(decorator);
- };
- var sale = new Sale();
- console.log(sale.getPrice()); //100
- sale.decorate('fedtax');
- console.log('after fedtax:' + sale.getPrice()); //after fedtax:105
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement