Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are jQuery objects inherently unique?

Reading about jQuery objects from here, it states that all jQuery objects are unique, even if they that "This is true even if the object was created with the same selector or contain references to the exact same DOM elements."

So for example, the following would equate to false:

$( "#logo" ) === $( "#logo" )

Why are jQuery objects all unique?

Thanks

like image 402
biddano Avatar asked Mar 10 '14 14:03

biddano


2 Answers

Because, essentially, jQuery is using the factory pattern which creates a new instance of a jQuery object from a selector each time you call it.

As these are different instances, they are not equal.

like image 196
Jamiec Avatar answered Nov 04 '22 00:11

Jamiec


Try following:

$( "#logo" ).get(0) === $( "#logo" ).get(0)

As far as I know this compares the original Javascript-DOM-Object like you get with e.g.

document.getElementById( "logo" ) === document.getElementById( "logo" )
like image 1
algorhythm Avatar answered Nov 03 '22 23:11

algorhythm