Actionscript:
-
var connect:Function = function(xp:Number, yp:Number, col:uint=0):Function{
-
graphics.lineStyle(0,col);
-
graphics.moveTo(xp, yp);
-
var line:Function = function(xp:Number, yp:Number):Function{
-
graphics.lineTo(xp, yp);
-
return line;
-
}
-
return line;
-
}
-
-
// draw a triangle
-
connect(200,100)(300,300)(100,300)(200, 100);
-
-
// draw a box
-
connect(100,100, 0xFF0000)(150,100)(150,150)(100, 150)(100,100);
This is one of those techniques that I never really get tired of. It's pretty useless, but fun to play around with every now and then. This draws the somewhat boring looking picture below:
[EDIT]
A few people pointed out that this could be simplified with arguments.callee... So here is an example... it does the same thing as the original code...
Actionscript:
-
var connect:Function = function(xp:Number, yp:Number, col:uint=0):Function{
-
graphics.lineStyle(0,col);
-
graphics.moveTo(xp, yp);
-
return function(xp:Number, yp:Number):Function{
-
graphics.lineTo(xp, yp);
-
return arguments.callee;
-
}
-
}
6 Comments
I would kill you if you wrote code like that for a project I was on.
ha!… I wonder if anyone has actually ever used code like that on a real project…
That’s pretty much exactly how the old AS2 Delegate class looks, if I remember correctly.
ah yes… the delegate class… that brings back some memories…
ohuuu : )
i’m in studing python and this is really in mood as pythononian dynamic
not really clean but tricky!
that is called closures
Common with every languages, but almost not used as most of us do not understand how they work.
Maybe a private method is a way to describe them; but without OOP !?