0

Can anyone explain me why in jQuery inside an event binding:

this === event.target

returns true

but

$(this) === $(event.target)

returns false?

Example Code:

$('#btnSubmit').on('click', function(event) {
    console.log( event.target === this); // true
    console.log( $(event.target) === $(this)); // false
})

Edit: I can't see an explanation for my question in the mentioned question

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Marc
  • 2,659
  • 3
  • 34
  • 41
  • 2
    Every time you call `$()` you're creating a new jQuery wrapper object. They may be equivalent, but they're not `===`. – Barmar May 29 '14 at 15:24
  • Try `[1, 2] === [1, 2]` for a similar effect. They're two arrays with the same contents, but they're not the same object. If you modify one of them, the other doesn't change. – Barmar May 29 '14 at 15:26
  • Because they contain the same values merely by coincidence. If every time you happened to create two arrays or objects that contain the same elements they were linked together, it would cause havoc. – Barmar May 29 '14 at 15:47

1 Answers1

0

this refers to native DOM Element

$(this) refers to jQuery wrapper object created by jQuery factory method $()

Also the equality test on different object will give false as explained by @Barmar in the comment

Murali Murugesan
  • 22,423
  • 17
  • 73
  • 120