Actionscript:
-
package {
-
import flash.display.Sprite;
-
public class Chaining extends Sprite{
-
public function Chaining(){
-
print(n(100).divide(2).plus(2));
-
// outputs 52
-
print(n(100).plus(n(10).multiply(2)));
-
// outputs 120
-
}
-
}
-
}
-
-
function print(n:Num):void{
-
trace(n.getValue());
-
}
-
function n(n:Number):Num{
-
return new Num(n);
-
}
-
-
class Num{
-
private var value:Number;
-
-
public function Num(n:Number):void{
-
value = n;
-
}
-
private function convert(n:*):Number{
-
if (n is Num) n = n.getValue();
-
return n;
-
}
-
public function getValue():Number{
-
return value;
-
}
-
public function plus(n:*):Num{
-
n = convert(n);
-
value += n;
-
return this;
-
}
-
public function minus(n:*):Num{
-
n = convert(n);
-
value -= n;
-
return this;
-
}
-
public function multiply(n:*):Num{
-
n = convert(n);
-
value *= n;
-
return this;
-
}
-
public function divide(n:*):Num{
-
n = convert(n);
-
value /= n;
-
return this;
-
}
-
}
This snippet is meant to be run as a document class. It shows how one might go about designing a class to make extensive use of function chaining.
2 Comments
Check this out -
http://drawlogic.com/2008/04/24/as3query-actionscript-port-of-jquery/
thanks for the link brendan. I’ve seen this before, very cool stuff… that same guy has a bunch of other interesting projects… some of which are here.