Created
February 2, 2013 12:11
-
-
Save takaheraw/4697043 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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