forEach, forIn
http://level0.kayac.com/2011/01/describetype_foreach.php (japanese blog)
/**
* Copyright 9re ( http://wonderfl.net/user/9re )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/mS6j
*/
package {
import flash.utils.describeType;
import flash.text.TextField;
import flash.display.Sprite;
// http://level0.kayac.com/2011/01/describetype_foreach.php (japanese blog)
public class FlashTest extends Sprite {
public function FlashTest() {
var tf:TextField = new TextField;
tf.width = tf.height = 465;
addChild(tf);
var a:Array = ['array_a','array_b','array_c'];
var o:Object = {a:'object_a',b:'object_b'};
var $a:A = new A;
log('--- forEach ---');
forEach(a, log);
forEach(o, log);
forEach($a, log);
log('--- forIn ---');
forIn(a, log);
forIn(o, log);
forIn($a, log);
function log(...o:Array):void {
tf.appendText((o ? o.join(' ') : "") + "\n");
}
}
}
}
import flash.utils.describeType;
function forEach(obj:*, callback:Function):void {
var typeInfo:XML = describeType(obj);
var key:String;
if (typeInfo.children().length() == 0 ||
typeInfo.@name == 'Array') { // object
for (key in obj) callback(obj[key]);
} else {
for each (key in typeInfo..variable.@name) callback(obj[key]);
}
}
function forIn(obj:*, callback:Function):void {
var typeInfo:XML = describeType(obj);
var key:String;
if (typeInfo.children().length() == 0 ||
typeInfo.@name == 'Array') { // object
for (key in obj) callback(key);
} else {
for each (key in typeInfo..variable.@name) callback(key);
}
}
class A {
public var var0:String = "A_a";
public var var1:String = "A_b";
}