Comparison Operators

     

The comparison operators are used to determine whether two operands are equal, or whether one operand is greater than or less than another. The eight comparison operators are as follows :

  • Equality ( == )

  • Inequality ( != )

  • Strict equality ( === )

  • Strict inequality ( !== )

  • Less than ( < )

  • Less than or equal to ( <= )

  • Greater than ( > )

  • Greater than or equal to ( >= )

The first four are also known as equality operators, and the last four as relational operators. All eight compare two strings or numbers and return true or false , depending on whether the relationship indicated in the comparison is accurate.

In addition, the relational operators return undefined if at least one of the operands is NaN . The equality and strict equality operators return false in this case, whereas the inequality and strict inequality operators return true .

Understanding Equality Operators

Both the equality ( == ) and strict equality ( === ) operators test for "sameness," but the equality operator performs datatype conversions when necessary before comparing. The strict equality operator insists that the operands already be of the same datatype, or else it returns false .

For instance, null == undefined is true, but null === undefined is false. Because null and undefined are of different datatypes, they are not strictly equal. However, the equality operator converts them both to 0, so they are equal.

The inequality ( != ) and strict inequality ( !== ) operators return the opposite of the equality and strict equality operators. Whenever the former returns true , the latter returns false , and vice versa.

The equality operator is perhaps the most commonly used operator, and also the source of one of the most common mistakes: using a single equal sign (the assignment operator) when you mean to use two equal signs (the equality operator).

Numbers are compared mathematically. For instance, these expressions all yield true :

 1 + 2 == 3 -20 < -1 80.5 != 80 

In contrast, these expressions all yield false :

 6 <= 5 5 >= 6 -200 > 0 

The "code points" of the ISO-8859 (Latin-1) or Unicode character set are used to compare strings. The code point is the number or "character code" associated with each character. For instance, these expressions all yield true :

 "A" < "a" // capital letters come before lowercase "A" > "1" // numbers come before capital letters "<" <= ">" // the "less than" sign comes before the "greater than" sign 

If you want to use the character code of a letter to compare it with a number, use the charCodeAt() function. For instance, the character code for a blank space is 32. Therefore, the following expression yields true :

 " ".charCodeAt(0) == 32 // true, the first (0) character is a blank 

This expression yields false :

 " " == 32 

Automatic Datatype Conversions for Comparisons

The expression " " == 32 is false because of the way the ActionScript interpreter performs automatic datatype conversion. For primitive datatypes, the interpreter converts operands to ensure that it ultimately compares either two strings or two numbers. Note that the interpreter makes temporary copies of the operands and converts the copies; it does not change the original data that is being compared.

A character string converts to a number only if it literally spells out a number. For example, the following yields true :

 "32" == 32 

In all other cases, strings are converted to NaN . If either operand (or both) is converted to NaN , the comparison yields false for all the comparison operators except != and !== . As counterintuitive as it seems, even NaN == NaN is a false expression, as far as the ActionScript interpreter is concerned ! By the same token, NaN != NaN is true. This makes comparison operators an extremely nonintuitive means of determining whether a datum is NaN . The typeof operator doesn't help either; it just yields number . To test whether a value is NaN , use the isNaN() function:

 x = NaN; trace(x); // NaN( trace(x == NaN); // false trace(typeof x); // number trace(isNaN(x)); // true 

The preceding discussion assumes two primitive datatypes. If just one of the operands in a comparison is a composite datatype ( object , array , or function ), the ActionScript interpreter invokes the valueOf() method of the operand. If valueOf() returns a primitive value, that value is used in the comparison. If not, the comparison yields false .

In Flash 5, the valueOf() method was used in comparing two composite types, as well. This is no longer true for equality/inequality comparisons in Flash MX.

In Flash MX, if both operands are composite types, equality/inequality comparisons are made by reference . This means they are equal only if they reference the same object .

Thus, if f1 and f2 refer to function literals, (f1 == f2) is true only if both refer to the same function literal, as is the case in the following example:

 myfunc = function (){}; f1 = myFunc; f2 = myFunc; trace(f1==f2); // true 

On the other hand, func1 and func2 in the following example are identical in form, but do not refer to the same function literal:

 function func1(){} function func2(){} trace(func1 == func2); // false 

Using Comparison Operators with Objects and Arrays

By default, >= , <= , > , and < do not yield meaningful results with objects and arrays as operands.

Newly created objects test unequal :

 trace(o == o2); // false trace(o != o2); // true 

However, the results of the following statements, taken together, would lead to the conclusion that the objects are equal:

 trace(o); // [object Object] trace(o2); // [object Object] trace(o <= o2); // true trace(o >= o2); // true trace(o > o2); // false trace(o < o2); // false 

For instance, if o >= o2 is true ( o is either greater than or equal to o2 ) and o > o2 is false (it's not greater than), o must be equal to o2 , right? (Wrong. You'll see why in a moment.)

Results for two newly created arrays are similar, except that they display nothing where objects display [object Object] .

These results, however, are simply artifacts of the way Flash tests for >= and <= . In both cases, under the covers, Flash actually uses < in the final comparison and then reverses the answer. For example, for o <= o2 , Flash asks, "Is o2 < o ?" If not, it concludes that o <= o2 must be true! For o >= o2 , Flash asks, "Is o < o2 ?" If not, it concludes that o >= o2 must be true!

These conclusions are valid, assuming numeric operands, or strings that can be reduced to numeric code points. In the case of objects, however, the interpreter converts both objects to NaN . The result of (NaN < NaN) is undefined:

 trace(typeof (NaN < NaN));  // undefined 

The ActionScript interpreter converts undefined to false in comparison results. So the interpreter reverses that result and deduces true for the original comparisons, in both cases!

Clearly, by default, >= , <= , > , and < do not produce generally useful results with objects or arrays as operands. However, you can create valueOf() methods for objects or arrays, and these methods can return numeric or string primitives that can usefully be compared using the >= , <= , > , and < operators.

Note that objects and arrays differ from functions in this respect. The valueOf() method does not come into play when comparing two functions. It plays a role only when comparing a function with a primitive datatype.


Movie clips have always been compared by their instance names , and this is still true in Flash MX. Only the equality/inequality comparisons yield meaningful results with movie clips. The >= , <= , > , and < operators yield undefined .

Automatic datatype conversion for comparisons favors numbers over other datatypes: If one operator is a number and the other is a string, Boolean, null , or undefined , the non-numeric operand is converted to a number. This includes non-numeric operands returned by valueOf() functions.

Deprecated Flash 4 Comparison Operators

The Flash 4 comparison operators eq , ne , lt , gt , le , and ge are equivalent to == , != , < , > , <= , and >= , except that lt , gt , le , and ge are string specific. As of Flash 5, the Flash 4 operators are "deprecated," meaning supported but not recommended, unless you're exporting to Flash 4 format.

You may find situations in which the deprecated objects work better than the newer ones. Rather than use the old ones, you might be wise to create new functions of your own, based on the new operators but compensating for whatever behavior is creating a problem.

Macromedia will drop support for deprecated operators and functions as soon as it is practical to do so. Programs using deprecated syntax live on borrowed time.



Using Macromedia Studio MX 2004
Special Edition Using Macromedia Studio MX 2004
ISBN: 0789730421
EAN: 2147483647
Year: N/A
Pages: 339

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