다음은 재생할 수있는 타임 라인 코드입니다.
import flash.display.MovieClip;
import flash.display.Sprite;
var object : Object;
var array : Array;
var number : Number;
var integer : int;
var uinteger : uint;
var string : String;
var xml : XML;
var regexp : RegExp;
var movieClip : MovieClip;
var array2 : Array = [];
var object2 : Object = {};
trace("uninstantiated Object =", object, "Boolean cast =", Boolean(object));
trace("uninstantiated Array =", array, "Boolean cast =", Boolean(array));
trace("uninstantiated Number =", number, "Boolean cast =", Boolean(number));
trace("uninstantiated int =", integer, "Boolean cast =", Boolean(integer));
trace("uninstantiated uint =", uinteger, "Boolean cast =", Boolean(uinteger));
trace("uninstantiated String =", string, "Boolean cast =", Boolean(string));
trace("empty String =", "", "Boolean cast =", Boolean(""));
trace("uninstantiated XML =", xml, "Boolean cast =", Boolean(xml));
trace("uninstantiated RegExp =", regexp, "Boolean cast =", Boolean(regexp));
trace("uninstantiated MoveClip =", movieClip, "Boolean cast =", Boolean(movieClip));
trace("instantiated Array with length 0 XML =", array2, "Boolean cast =", Boolean(array2));
trace("instantiated Object with no properties =", object2, "Boolean cast =", Boolean(object2));
trace("uninstantiated index of instantiated array =", array2[4], "Boolean cast =", Boolean(array2[4]));
trace("uninstantiated property of instantiated object =", object2[ "color" ], "Boolean cast =", Boolean(object2[ "color" ]));
try
{
//will throw an error
trace("uninstantiated property of uninstatinated class =", movieClip.x, "Boolean cast =", Boolean(movieClip.x));
}
catch (e : Error) {}
/*
output:
uninstantiated Object = null Boolean cast = false
uninstantiated Array = null Boolean cast = false
uninstantiated Number = NaN Boolean cast = false
uninstantiated int = 0 Boolean cast = false
uninstantiated uint = 0 Boolean cast = false
uninstantiated String = null Boolean cast = false
empty String = Boolean cast = false
uninstantiated XML = null Boolean cast = false
uninstantiated RegExp = null Boolean cast = false
uninstantiated MoveClip = null Boolean cast = false
instantiated Array with length 0 XML = Boolean cast = true
instantiated Object with no properties = [object Object] Boolean cast = true
uninstantiated index of instantiated array = undefined Boolean cast = false
uninstantiated property of instantiated object = undefined Boolean cast = false
As you can see, in general, you can just test for instantiation by using
if (variableName) ...
But this will fail if you want to think of instantiated-but-empty Arrays and Objects
as false. Note that it will also fail, generating an error, if you check for properies
of uninitialized classes, e.g.
var sprite : Sprite;
if (sprite.x) trace("found x"); //error, because sprite = null and null doesn't have an x property.
You can write a more liberal test like this:
*/
function exists(value : *) : Boolean
{
var result : Boolean = true;
if (! value) return false;
if (value is Array && ! value.length) return false;
if (value.constructor.toString() == "[class Object]")
{
result = false;
for each (var prop in value) { result = true; break; }
}
return result;
}
//tests
trace("exists(object) =", exists(object));
trace("exists(array) =", exists(array));
trace("exists(number) =", exists(number));
trace("exists(integer) =", exists(integer));
trace("exists(uinteger) =", exists(uinteger));
trace("exists(string) =", exists(string));
trace("exists('') =", exists(""));
trace("exists(xml) =", exists(xml));
trace("exists(regexp) =", exists(regexp));
trace("exists(movieClip) =", exists(movieClip));
trace("exists(array2) =", exists(array2));
trace("exists(object2) =", exists(object2));
trace("exists(array2[4]) =", exists(array2[4]));
trace("exists(object2[ 'color' ]) =", exists(object2[ "color" ]));
var sprite : Sprite = new Sprite();
trace("INSTANTIATED: exists(sprite) = ", exists(sprite));
var object3 : Object = { color : 0xFFFFFF };
trace("INSTANTIATED OBJECT WITH PROPERTY: exists(object3) =", exists(object3));
var array3 : Object = [ 1 ];
trace("INSTANTIATED ARRAY WITH LENGTH: exists(array3) =", exists(array3));
/*
output:
exists(object) = false
exists(array) = false
exists(number) = false
exists(integer) = false
exists(uinteger) = false
exists(string) = false
exists('') = false
exists(xml) = false
exists(regexp) = false
exists(movieClip) = false
exists(array2) = false
exists(object2) = false
exists(array2[4]) = false
exists(object2[ 'color' ]) = false
INSTANTIATED: exists(sprite) = true
INSTANTIATED OBJECT WITH PROPERTY: exists(object3) = true
INSTANTIATED ARRAY WITH LENGTH: exists(array3) = true
One more note: be careful with ints and uints. They are always initialized. Whereas other variables
point to null, undefined or NaN until you give them values, ints and uints are automatically intialized
to zero. There is not way to have an int that doesn't point to a value:
var num : Number;
var i : int;
trace(num); // NaN
trace(i); //0
In general, that's okay, because 0 == false:
var i : int;
if (i) trace("false"); //false
But I've mixed myself up before with code like this:
var color : int = getColorValueFromXML();
if (color) drawCircleWithColor(color);
In the above code, I only want to draw a circle if color is initialized.
However, color will ALWAYS bee initialized, to zero if not to anything else.
But what if getColorValueFromXML() returns 0x00000. We think of that as the
color black. But I see a black circle, because 0x000000 is also the same
as 0. And, in a conditional, 0 parses as false:
if (0x000000) drawCircleWithColor(); //drawCircleWithColor won't run!
*/
감사합니다. 패트릭 감사합니다.이 두 옵션은 더 깨끗하게 보입니다. 또한 값 유형이 설정되어있는 경우 알지 못했습니다. String는 null을 반환하고 정의되지 않은 문자열을 반환합니다. AS3에서 뭔가 새로운 것인가? undefined로 작업 한 AS2 코드 중 일부는 항상 확인 했습니까? – StephenAdams