2007年5月22日星期二

Changes in typeof

The typeof operator lets you determine the basic type of any value. Note that this does not give you actual class association information, but only provides a simplistic indication of the type of variable its used with. For more information regarding specific class relation, use instanceof, getQualifiedClassName, or describeType.

In ActionScript 1 and 2, typeof returned the following values:

  • boolean
  • function
  • movieclip
  • null
  • number
  • object
  • string
  • undefined


In ActionScript 3, typeof returns:

  • boolean
  • function
  • number
  • object
  • string
  • xml
  • undefined


Notice that MovieClip instances are, in AS3, no longer recognized by typeof. They are now seen as objects. Additionally, there is no null value (also seen as an object) and xml is seen as being of type xml.

The new number types in AS3, int and uint, when used with typeof both return number.

Also, primitive values (boolean, number, and string) created with their constructors and the new keyword are now recognized as those primitive types by typeof and not as objects as they were in AS1 and AS2.

ActionScript Code:

// AS1 & AS2
trace(typeof new XML()); // object

trace(typeof my_mc); // movieclip

trace(typeof null); // null

trace(typeof true); // boolean
trace(typeof 1); // number
trace(typeof ""); // string

trace(typeof new Boolean()); // object
trace(typeof new Number()); // object
trace(typeof new String()); // object

ActionScript Code:

// AS3
trace(typeof new XML()); // xml

trace(typeof my_mc); // object

trace(typeof null); // object

trace(typeof true); // boolean
trace(typeof 1); // number
trace(typeof ""); // string

trace(typeof new Boolean()); // boolean
trace(typeof new Number()); // number
trace(typeof new String()); // string

trace(typeof int(1)); // number
trace(typeof uint(1)); // number

1评论:

Typhone 说...

最近寫代碼時一直被typeof困惑著,因為我一直是用typeof()這樣子的,後來改用了AS3裡的一個新函數getQualifiedClassName才搞定如何返回變量的類型,沒想typeof只是變成這樣子,又走彎路了