Skip to main content
Inspiring
May 1, 2012
Question

when is stricly equal not equal?

  • May 1, 2012
  • 1 reply
  • 1037 views

at the end of a lengty debugging session, I have this situation

public dynamic class AA extends Array

{

     public function close():void

     {     this[this.length-1] = this[0];     // if the array describes a circular structure, the last element definitely should be the first one

     }

}

the AA class members are, by design, instances of class A (or possibly A2 that extends A). In this case, they are definitely

class A

I call close(), and later verify that the first and last still seem to be identical (at least as far as A's toString() traces out)

In a different part of the program, I have

var aa:AA = ....

for(var i:int = 0 ; i < aa.length ; i++)

{     var a:A = aa;

     if(i != 0 && a == aa[0])

          break;     // ignore last element if it happens to be the "end" of a ring, rather than a chain

}

the == returned false.....

When I changed the code to use ===, the comparison returned true as expected.

Of course, both == and === define object equality the same way. So, what could be the difference?

This topic has been closed for replies.

1 reply

May 1, 2012

>>Of course, both == and === define object equality the same way. So, what could be the difference?

They don't actually, === will return true only if both items are of the same type and value, where == goes by value alone.

Inspiring
May 2, 2012

Hi dmennenoh,

I was referring to these statements

-- description of == --

Variables representing objects, arrays, and functions are compared by reference. Two such variables are equal if they refer to the same object, array, or function. Two separate arrays are never considered equal, even if they have the same number of elements.

-- description of === --

The strict equality (===) operator is the same as the equality (==) operator in three ways:

  • Numbers and Boolean values are compared by value and are considered equal if they have the same value.
  • String expressions are equal if they have the same number of characters and the characters are identical.
  • Variables representing objects, arrays, and functions are compared by reference. Two such variables are equal if they refer to the same object, array, or function. Two separate arrays are never considered equal, even if they have the same number of elements.

I was trying to identify the reference created in function close