//The class we're going to decorate
function Macbook(){
this.cost = function(){
return 1000;
};
}
function Memory(macbook){
this.cost = function(){
return macbook.cost() + 75;
};
}
function BlurayDrive(macbook){
this.cost = function(){
return macbook.cost() + 300;
};
}
function Insurance(macbook){
this.cost = function(){
return macbook.cost() + 250;
};
}
// Sample usage
var myMacbook = new Insurance(new BlurayDrive(new Memory(new Macbook())));
console.log( myMacbook.cost() );
function ConcreteClass(){
this.performTask = function()
{
this.preTask();
console.log('doing something');
this.postTask();
};
}
function AbstractDecorator(decorated){
this.performTask = function()
{
decorated.performTask();
};
}
function ConcreteDecoratorClass(decorated){
this.base = AbstractDecorator;
this.base(decorated);
this.preTask = function()
{
console.log('pre-calling..');
};
this.postTask = function()
{
console.log('post-calling..');
};
}
var concrete = new ConcreteClass();
var decorator1 = new ConcreteDecoratorClass(concrete);
var decorator2 = new ConcreteDecoratorClass(decorator1);
decorator2.performTask();