Robert
Robert

Reputation: 10380

jQuery objects comparison

According to the jQuery documentation, "Not All jQuery Objects are Created ===."

"An important detail regarding this "wrapping" behavior is that each wrapped object is unique. This is true even if the object was created with the same selector or contain references to the exact same DOM elements."

documentation

I know how to work around this but why is this the case? Is this some specific way that JavaScript behaves?

Upvotes: 0

Views: 35

Answers (1)

nrabinowitz
nrabinowitz

Reputation: 55688

Yes. Every object in JS is unique, in that o1 === o2 will not be true unless o1 and o2 are pointers to the same object.

{ foo: 1 } === { foo: 1 }; // false

So jQuery objects simply follow this same rule:

var jq1 = $('.foo');
var jq2 = $('.foo');
jq1 === jq2; // false

The only exception is if you have variables that actually point to the same jQuery object:

var jq3 = jq1;
jq3 === jq1; // true

Upvotes: 4

Related Questions