Created
February 14, 2010 10:46
-
-
Save ongaeshi/303950 to your computer and use it in GitHub Desktop.
配列、オブジェクト等をきちんとソースコードと同じ状態で表示してくれる, trace2, to_s
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
public class Lib { | |
static private function to_s_array(array:Object):String | |
{ | |
var result:String = ""; | |
result += "["; | |
for (var i:int = 0; i < array.length; i++) { | |
result += to_s(array[i]) ; | |
if (i != array.length - 1) | |
result += ", "; | |
} | |
result += "]"; | |
return result; | |
} | |
static private function to_s_object(x:Object):String | |
{ | |
var result:String = ""; | |
var isSecond:Boolean = false; | |
result += "{"; | |
for (var key:String in x) { | |
if (isSecond) | |
result += ", "; | |
else | |
isSecond = true; | |
result += key + ":" + to_s(x[key]); | |
} | |
result += "}"; | |
return result; | |
} | |
static public function to_s(x:Object):String | |
{ | |
if (x == null) | |
return "null"; | |
else if (x.constructor === Object) | |
return to_s_object(x); | |
else if (x.constructor === Array) | |
return to_s_array(x); | |
else if (typeof x == "string") | |
return '"' + x + '"'; | |
else | |
return x.valueOf(); | |
} | |
static public function trace2(x:Object):void | |
{ | |
trace(to_s(x)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var o:Object; | |
o = 121; | |
Lib.trace2(o); // -> 121 | |
o = "This is String."; | |
Lib.trace2(o); // -> "This is String." | |
o = [1, 2, 3, 4, 5]; | |
Lib.trace2(o); // -> [1, 2, 3, 4, 5] | |
o = { x:1, y:2 }; | |
Lib.trace2(o); // -> {x:1, y:2} | |
o = { x:10, y:"abcdef", z:[300, 200, 100] }; | |
Lib.trace2(o); // -> {z:[300, 200, 100], x:10, y:"abcdef"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment