views:

294

answers:

3

What is the safest way to determine if a Javascript object is an event?

+2  A: 

It's fairly good practice to probe possibly "unknown" objects for the properties and methods you expect to find.

So, assume you have got an event object, and probe it before acting on it, e.g.

if (event.target)
{
   //looks like we're an event, hide the target
   var e=$(event.target);
   e.hide();
}

It's important to note that I'm NOT suggesting you test for 'target' to see if its an event: you're testing for target because you're about to use that property. What I'm driving at is that instead of trying to figure out whether an object is event, probe the object to see whether it is going to behave in the way you expect, then use those behaviours.

Code like this should degrade gracefully on browsers with different support, or let you take advantage of browser-specific extensions, e.g.

if (event.initKeyEvent)
{
    //gecko 1.9+
    event.initKeyEvent(...)
}
Paul Dixon
Again, the event example will not work in IE, which has a srcElement property instead of target.
Tim Down
You've misunderstood - you're not testing for 'target' to see if its an event, you're testing for target *because you're about to use that property*.What I'm driving at is that instead of trying to figure out whether an object is event, probe the object to see whether it is going to *behave* in the way you expect, then use those behaviours.
Paul Dixon
Fair enough. I don't think it really answers the original question, but then I can't see why you'd ever need to test if an object is an event anyway.
Tim Down
Now here's an interesting thought... I need only three properties of the Event object (if it really is an object) so I'll just check if it has all three an make sure their types are the ones I need. If the user is idiot enough to send me a fake object with usable values, it is entirely her fault.Thanks!
Tom
A: 

you can use getAttribute of an object (event) to check whether the object is an event or just an object.

try{
     event.getAttribute('type')
     isEvent=true;
}
catch(ex)
{
     isEvent=false;     
}

or if you have the event.type constants then you can loop over them and check whether ihe instance is a event or not

EventNames = new Array();
EventNames[0]="load";  //all event names are kept in this array
for(i=0;i<EventNames;i++)
{
  if (event.type==EventNames[i])
         isEvent=true;
 }

EDIT

cant we just check

try{    
if (event.type=="")
        isEvent=false;
}
catch(ex)
{
     isEvent=false;
}

am sure all events must come with an valid event type.. i think this should work...

Cheers

Ramesh Vel

Ramesh Vel
No. Event objects in browsers other than IE do not have a getAttribute method.
Tim Down
okie.. is there any equivalent in firefox for getAttribute() method..??
Ramesh Vel
getAttribute is a standard method for DOM Elements which is available in all browsers. It is a bug that it even exists for Event objects in IE, and certainly not something you should rely on. If you only mean to check for the presence of a ‘type’ property, just do `var isEvent= thing.type!==undefined;`. But ‘type’ is a poor choice of property to check, since *many* other DOM objects have a ‘type’ property.
bobince
(In general, you should avoid all use of getAttribute for HTML documents, as it has serious bugs in IE. Accessing the JavaScript/DOM-HTML properties is much more reliable, and easier to read too.)
bobince
@bobince, i have updated my answer. can u check that...
Ramesh Vel
A: 

I don't know if there's a sure-fire way to do that, but I think your best shot is duck-typing.

Anyway, depending on the situation you could check if a given object has the expected properties that you want to use, just like Paul pointed out.

Pablo Cabrera