6

I have run the following lines in my console (once a jquery script has been loaded), and received the following results:

$(this)
> [Window]
$(this) != $(this)
> true
$(this) == $(this)
> false
$(this) === $(this)
> false

And I don't know what steps to take to figure out what is going on. My guess is that there is some object that holds a time based value which is changing, but I wonder if it's something different. I will try to compare the values in the meantime, but I was hoping someone might understand what is going on here.

Edited to address the point that I was unaware of the underlying implementation of $(arg). I did not know that it returned a new reference object. Therefore, I don't believe this is a duplicate of "How to determine equality for two JavaScript objects?".

NicholasFolk
  • 1,021
  • 1
  • 8
  • 14
  • http://stackoverflow.com/questions/201183/how-to-determine-equality-for-two-javascript-objects – j08691 Feb 18 '17 at 19:44
  • 1
    _"I will try to compare the values in the meantime"_ What are you trying to determine? – guest271314 Feb 18 '17 at 19:45
  • 1
    @guest271314 Comparing to see if all properties are equal in value. I see now that javascript equals evaluates based on equality alone AND that $(this) returns a new reference each time it is called. Therefore, baao, I don't think this is a duplicate. I was unaware that $(this) returned a new reference object to "this" each time, and that is not addressed in your linked question. – NicholasFolk Feb 18 '17 at 19:49
  • 1
    The previous downvote seemed unfair. It's a legitimate question. – James Drinkard Feb 18 '17 at 19:49

1 Answers1

7

Using $() returns an instance of jQuery. So, you create one instance with this, and another instance of this, you have two separate instances. Even though they share the same reference to this, the instances are not the same, and those are what is being compared.


It might help to have a visual example, and might make things a little more clear. jQuery operates like a class. So let's use an very simple example where for example's sake, the function $() doesn't exist:

class jQuery {
    constructor(element) {
        this.element = element;
    }
}

var obj1 = new jQuery(this);
var obj2 = new jQuery(this);

console.log(obj1 === obj2); // false

Both of those are using the exact same argument (this) to create a new "jQuery" object. But, once again, obj1 is a completely different instance than obj2. They both have their own unique place in memory.

KevBot
  • 17,900
  • 5
  • 50
  • 68
  • 3
    +1 - Furthermore, if you want to compare the values, use `this === this`. To clarify, when you wrap it with `$()`, an instance of a [jQuery object](https://learn.jquery.com/using-jquery-core/jquery-object/) is returned (you might want to clarify that in your answer). – Josh Crozier Feb 18 '17 at 19:51
  • Marked as the answer because it addresses the root of the question, which is essentially "what does $(...) return (or do under the hood)?". – NicholasFolk Feb 18 '17 at 19:57
  • 1
    @NicholasFolk, awesome. I also went ahead and added a more visible example of how that works due to JoshCrozier's comment. – KevBot Feb 18 '17 at 19:59
  • @JoshCrozier, thank you for the suggestion to add clarification! – KevBot Feb 18 '17 at 20:07