In jQuery, why does $ (this) == $ (this) return false?

I ran the following lines in my console (after loading the jquery script) and got the following results:

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

And I do not know what steps need to be taken to find out what is happening. I suppose there is some kind of object that contains a value based on time that is changing, but I wonder something else. I will try to compare the values ​​at the same time, but I was hoping that someone could understand what was going on here.

Edited to indicate that I did not know about the basic implementation of $ (arg). I did not know that he returned a new reference object. Therefore, I do not believe that this is a duplicate of "How to determine equality for two JavaScript objects?".

+6
source share
1 answer

Using $() returns a jQuery instance. So, you create one instance with this , and another instance of this , you have two separate instances. Although they have the same reference to this , the instances do not match, and this is what is being compared.


This can help get a good example and make things more understandable. jQuery works like a class. So let me use a very simple example where, for example, sake, the $() function does not 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 them use the same argument ( this ) to create a new jQuery object. But, again, obj1 is a completely different instance than obj2. They both have their own unique place in memory.

+6
source

Source: https://habr.com/ru/post/1015088/


All Articles