Skip to content

Commit

Permalink
-constructor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
L0stSoul authored and L0stSoul committed Nov 15, 2011
1 parent 1c1a538 commit 83e6261
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion Patterns/Scripts/Decorator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
function Ball( param )

function Ball( param )
{
this._radius = param.radius;
this._color = param.color;
Expand All @@ -13,12 +14,15 @@ Ball.prototype =
inc: function(){ this._radius += this.INCREMENTATION_STEP }
}

// Decorator_A
function StripedBall( ball )
{
this._ball = ball
}
StripedBall.prototype =
{
constructor: StripedBall,

draw: function()
{
this._ball.draw();
Expand All @@ -36,6 +40,8 @@ function SpeckledBall( ball )
}
SpeckledBall.prototype =
{
constructor: SpeckledBall,

draw: function()
{
this._ball.draw();
Expand All @@ -47,6 +53,7 @@ SpeckledBall.prototype =
}
}

console.log("*heavy decorator*");
var ball1 = new SpeckledBall( new StripedBall( new Ball({ radius:100, color:"red"})));
var ball2 = new StripedBall( new SpeckledBall( new Ball({ radius:100, color:"green"})));

Expand All @@ -55,3 +62,40 @@ ball1.inc();
ball1.draw();

ball2.draw();

//Decorator_B
function MakeStripedBall( ball )
{
var function_name = "draw";
var prev_func = ball[ function_name ];

ball[ function_name ] = function()
{
prev_func.apply( this, arguments )
console.log("and with stripes");
};

return ball;
}
function MakeSpeckledBall( ball )
{
var function_name = "draw";
var prev_func = ball[function_name];

ball[function_name] = function ()
{
prev_func.apply(this, arguments)
console.log("and with dots!");
};

return ball;
}
console.log("*light decorator*");
var ball3 = MakeStripedBall( MakeSpeckledBall( new Ball({ radius: 150, color: "blue" })));
var ball4 = MakeSpeckledBall(MakeStripedBall(new Ball({ radius: 150, color: "blue" })));

ball3.draw();
ball3.inc();
ball3.draw();

ball4.draw();

0 comments on commit 83e6261

Please sign in to comment.