4.9 Null

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 4.  Primitive Datatypes

Intellectually, the null type is nearly identical to the undefined type. Like the undefined datatype, the null datatype is used to represent a lack of data and has only one legal value, the primitive value null. However, the null value is not assigned by the interpreter automatically; it must be assigned by us deliberately.

We assign null to a variable, array element, or object property to indicate that the specified data container does not contain a legal number, string, Boolean, array, or object value. For example, we might assign an initial value of null to an object property to indicate that it exists but has not yet been assigned a useful value.

To remove a variable or object property, you should use the delete operator rather than assigning the value null to the variable or property.

Note that null compares equal only to itself and undefined:

null =  = undefined;   // true null =  = null;        // true

To guarantee that a variable or property contains null (rather than undefined), check its datatype using the typeof operator:

var x = null; if (typeof x =  = "null") {   trace("x is null"); }

Alternatively, as of Flash Player 6, we can use the strict equality operator (= = =), as follows:

if (x =  =  = null) {   trace("x is null"); }

Though null and undefined are similar, their roles in ActionScript programs are distinct. The null value is used by programmers to indicate that a variable or property has been left undetermined intentionally. By contrast, the undefined value is assigned by the interpreter automatically whenever no other value is available. Consider a photo album application that displays a series of images sequentially. The application is initialized with title and description variables. If we choose not to bother with a description for a particular album, we set description to null.

var title = "My Holiday Photos"; var description = null;

The ensuing initialization code can then tell that description was omitted intentionally.

// If there's a valid description... if (description !=  = null) {   // ...create description text field. }

If we use undefined instead of null:

var title = "My Holiday Photos"; var description = undefined;

the initialization code has no way to tell whether the interpreter set description's value automatically or we set it intentionally. The distinction is subtle, but it is valuable in large programs. Here we expand our earlier initialization code to display an error message when no description value has been set.

// If there's a valid description... if (description =  =  = undefined) {   trace("WARNING: No description value was specified!"); } else if (description !=  =  = null) {   // ...create description text field. }

By distinguishing between undefined and null in our initialization code, we can provide accurate debugging feedback and catch potential programmer oversight. For another example of real-world null usage, see the TextFormat class's constructor parameters in the Language Reference.

     



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net