4

Related: Check if object is a jQuery object

I have an object x, and after running console.log(x) Chrome's debugger shows me this:

enter image description here

How can I check if x is a jQuery.Event using JavaScript? (I've tried Object.getPrototypeOf, toString and obj.constructor).

Community
  • 1
  • 1
Sam Selikoff
  • 12,366
  • 13
  • 58
  • 104
  • 2
    `x instanceof jQuery.Event`? – gen_Eric Aug 06 '14 at 19:10
  • awesome, thanks! Is there any way to get the string "jquery.Event" just from the object? – Sam Selikoff Aug 06 '14 at 19:16
  • Why do you need it as a string? – gen_Eric Aug 06 '14 at 19:17
  • @RocketHazmat Can't think of a reason right now, was just curious. – Sam Selikoff Aug 06 '14 at 19:26
  • @Ian - Why did you re-open this? – j08691 Aug 06 '14 at 21:15
  • @j08691 It might sound silly, but I felt the question was different enough, specifically because it refers to the jQuery **event** object. It turns out that the answer is fundamentally the same as the duplicate you linked, but the question was about something different (although **quite close**). I don't know, I just feel like it isn't right for someone searching for this question to be redirected to a duplicate with an answer for something else. I'd hope they'd be able to figure out that `instanceof` should be applied in the same way, but it's possible they wouldn't. I don't know, I'm picky – Ian Aug 07 '14 at 17:35
  • fwiw I was aware of `instanceof` and it was non-obvious that this could be used on "subclasses" of the main jQuery export. – Sam Selikoff Aug 07 '14 at 17:38

1 Answers1

5

Agreeing @Rocket's comment: instanceof is the way to go. You can try this on SO's website in the JS console

var x = $.Event()
x instanceof $.Event

More information can be found in this eerily similar post: https://stackoverflow.com/a/1853246/1883547

Community
  • 1
  • 1
adambullmer
  • 952
  • 9
  • 29