Skip to content

Instantly share code, notes, and snippets.

@takaheraw
Created February 2, 2013 12:11
Show Gist options
  • Save takaheraw/4697043 to your computer and use it in GitHub Desktop.
Save takaheraw/4697043 to your computer and use it in GitHub Desktop.
function Sale(price){
this.price = price;
this.decorators_list = [];
}
Sale.decorators = {};
Sale.decorators.fedtax = {
getPrice: function(price){
return price * 2
}
};
Sale.decorators.quebec = {
getPrice: function(price){
return price * 3
}
};
Sale.decorators.money = {
getPrice: function(price){
return price * 4;
}
};
Sale.prototype.decorate = function(decorator) {
this.decorators_list.push(decorator);
};
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;
};
var sale = new Sale(100);
sale.decorate('fedtax');
sale.decorate('quebec');
sale.decorate('money');
sale.getPrice();
function Parent(name){
this.name = name || 'Adam';
}
Parent.prototype.say = function(){
return this.name;
}
function Child(name){
Parent.apply(this, arguments);
}
Child.prototype = new Parent();
var kid = new Child("Patrick");
console.log(kid.name);
console.log(kid.say());
delete kid.name;
console.log(kid.say());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment