0

What is the difference between $ , this and $(this) in javascript fundamentals?

Also I want to know about their usage

dilwaria
  • 33
  • 8
  • Are you sure? This is not a duplicate of the question imo @Rayon – eisbehr Sep 20 '16 at 11:45
  • @eisbehr – In what context ? How ? – Rayon Sep 20 '16 at 11:45
  • Question is about `$` vs `$(this)`, duplicate about `this` vs `$(this)`. That's a difference. @Rayon – eisbehr Sep 20 '16 at 11:46
  • @eisbehr – Are you trying to say that `$` and `$(this)` or `this` are somehow related ? Could we compare `$` with `this` or `$(this)` ? – Rayon Sep 20 '16 at 11:47
  • Exactly this is the point. `$(this)` and `this` can be compared, because these both references a DOM object, one of them wrapped by jQuery object. So this question (*your duplicate*) has nothing in common with this question here. The duplicate will not helping someone to understand, that `$` and `$(this)` are totally different ... So as I said, this is not a duplicate of the question you marked here. @Rayon – eisbehr Sep 20 '16 at 11:50
  • @Rayon i dont want the difference but what is the usage and everything about $, this and $(this) and the duplicate answer does not have that. – dilwaria Sep 20 '16 at 11:50
  • @dilwaria – Your question does ask something about `"difference"` – Rayon Sep 20 '16 at 11:52
  • 1
    @eisbehr – As you said, _"$(this) and this can be compared"_, Provided dupe has __8__ answers to highlight those differences.. [___This___](http://stackoverflow.com/a/3714581/1746830) is the closest solution IMO.. – Rayon Sep 20 '16 at 11:54

1 Answers1

0

In short (assuming jQuery is included, of course):

  • $ is a variable of window containering the jQuery library itself.
  • this is a variable containing a DOM element.
  • $(this) casts the element this to an jQuery object.

So basically you can't talk about a difference. The one is the whole library itself and the other is just using the library ...

Another example to be clear:

var $ = function(param) {};
var elem = this;

$        // the library
elem     // the element
$(elem); // the usage
eisbehr
  • 12,243
  • 7
  • 38
  • 63
  • Sir, what do you mean by variable of window , it means Dom only or what ? – dilwaria Sep 20 '16 at 11:41
  • `$` is the same as `window.$`, you can use both, it is the same. Globally registered variables are always a property of the `window` object. @dilwaria – eisbehr Sep 20 '16 at 11:42
  • _"$ is the variable of window containering the jQuery library itself."_ Little confusing... `$` is not variable(key) of window object unless `jQuery` is included.. – Rayon Sep 20 '16 at 11:43
  • Because the question is tagged `jQuery` and the question goes straight to `jQuery`, I'm pretty sure we can assume that we talking about using `jQuery`. ;) @Rayon – eisbehr Sep 20 '16 at 11:44
  • @eisbehr – Fair enough... But _"$ is the variable of window"_ That is for sure.... – Rayon Sep 20 '16 at 11:46