-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorator.js
More file actions
39 lines (31 loc) · 752 Bytes
/
decorator.js
File metadata and controls
39 lines (31 loc) · 752 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// There is an example of 'decorator' pattern.
// More information here - http://javascript.info/tutorial/decorators
function Sale (price) {
this.price = price || 100;
}
Sale.prototype.getPrice = function() {
return this.price
};
Sale.decorators = {};
Sale.decorators.fedtax = {
getPrice: function () {
var price = this.uber.getPrice();
price += price * 5 / 100;
return price;
}
}
Sale.prototype.decorate = function(decorator) {
var F = function () {},
overrides = this.constructor.decorators[decorator],
i,
newobj;
F.prototype = this;
newobj = new F();
newobj.uber = F.prototype;
for( i in overrides ) {
newobj[i] = overrides[i];
};
return newobj;
};
var sale = new Sale(50);
sale.decorate('fedtax').getPrice(); // 52.5