Michael said:
Is it though? Surely two references to the same object, host
object or not, should still compare as equal? I realise that
host objects aren't governed by ECMA-262, but the equality
operator is and it would seem that that's decisive in this case.
If I had been more awake I might have put two and two together last
night and realised that this is not an issue with the comparison
operator, or an inability to compare the event objects. Neither would be
a satisfactory state of affairs, fortunately neither are explanations
for the phenomenon.
Various browsers have made properties available that when read return
unique objects each time they are read. This is likely to mean that
behind the scenes there is a 'getter' for the property that creates a
new object whenever a script reads the property. This appears to be the
case with the window.event property in IE.
Tests that would help decide whether this is the case might be;
1. Assigning a reference to an event object to a local variable and then
copying that reference to another local variable. Both local variables
must be references to the same object so if the object cannot be
compared with itself as true, or the comparison operator was not
functioning, then you might still get an equality result of false, but
you actually get true.
2. Create and assign a value to a new property of the event object
referred to by one of these two variables. Read the property back to see
if it was successfully assigned and then try to read the property from
the window.event object. Then re-confirm that the object referred to by
the variables still has the assigned property.
The second test successfully assigned the value to a property of the
event object referred to by the local variable, but that property was
subsequently missing from an object retrieved using the unqualified
Identifier - event -, while still being a property of the object
referred to by the local variable after is was found to be missing
from - event -. I would conclude that each read of the value of the
global event property within an event handler returns a reference to a
different (but otherwise identical) event object, and that is why they
do not compare as being the same object.
Test code:-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"
http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
</head>
<body>
<div>
<form action="">
<div>
<p>
<input type="button" value="Form Button"
onclick="
var st = 'window.event == event -> '
st += (window.event == event);
st += '\nevent == event -> '+(event == event);
st += '\nwindow.event == window.event -> '
st += (window.event == window.event);
var e1 = event;
var e2 = e1;
st += '\ne1 == e2 -> '+(e1 == e2);
e1.expndo = 'hellow'
st += '\ne1.expndo -> '+(e1.expndo);
st += '\nevent.expndo -> '+(event.expndo);
st += '\ne1.expndo -> '+(e1.expndo);
st += '\ne2.expndo -> '+(e2.expndo);
alert(st);
">
</p>
</div>
</form>
</div>
</body>
</html>
Richard.