52

I saw some code around the web that uses the following statement

if ($($(this)).hasClass("footer_default")) {
      $('#abc')
        .appendTo($(this))
        .toolbar({position: "fixed"});
    }

What is the use of $($(this)) and why is that necessary here?

12
  • 3
    Change it to just $(this), It will give the same result
    – Adil
    Commented Feb 10, 2014 at 7:18
  • 74
    It means some moron figured he'd be safe and wrap it twice.
    – adeneo
    Commented Feb 10, 2014 at 7:18
  • 14
    It means - "Who let that code get committed?" Commented Feb 10, 2014 at 7:21
  • 5
    Also note that it is more efficient to assign $(this) to a local variable once instead of doing $(this) multiple times.
    – Alvin Wong
    Commented Feb 10, 2014 at 10:16
  • 9
    It's what we call superstitious code.
    – Boann
    Commented Feb 10, 2014 at 12:24

6 Answers 6

78

Yes, $($(this)) is the same as $(this), the jQuery() or $() function is wonderfully idempotent. There is no reason for that particular construction (double wrapping of this), however, something I use as a shortcut for grabbing the first element only from a group, which involves similar double wrapping, is

$($('selector')[0])

Which amounts to, grab every element that matches selector, (which returns a jQuery object), then use [0] to grab the first one on the list (which returns a DOM object), then wrap it in $() again to turn it back into a jQuery object, which this time only contains a single element instead of a collection. It is roughly equivalent to

document.querySelectorAll('selector')[0];, which is pretty much document.querySelector('selector');

10
  • 1
    Thanks. I think this is what I was looking for. I have seen this throughout the web, now I am more aware of cases related to double wrapping.
    – guest
    Commented Feb 10, 2014 at 7:38
  • 2
    Not exactly idempotence. Idempotent function always return the same if given the same input, so a pure function is naturally idempotent. For instance, square(x) is idempotent, but calling square(square(x)) will return a different result to square(x). In this sense jQuery is rather good at equivocating, rather that idempotence.
    – Owen
    Commented Feb 10, 2014 at 11:43
  • 3
    Frankly, I find $($('selector')[0]) significantly harder to read, compared to $('selector:eq(0)') or even $('selector:first'), and it will perform worse because of the double "constructor".
    – UweB
    Commented Feb 10, 2014 at 12:25
  • 8
    Owen, I believe you are confusing idempotence with injection. Check Idempotence and Injection. Anyway, $() is not generally idempotent, but if the argument is a jquery object it will return the object it received. Commented Feb 10, 2014 at 12:29
  • 3
    I personally prefer $('selector').eq(0) if I'm trying to retrieve only the first element as a jquery object
    – George
    Commented Feb 10, 2014 at 15:54
29

You can wrap $ as many times as you want, it won't change anything.

If foo is a DOM element, $(foo) will return the corresponding jQuery object.

If foo is a jQuery object, $(foo) will return the same object.

That's why $($(this)) will return exactly the same as $(this).

8

There is no specific need for double-wrapping and $($(this)) is exactly the same as $(this).

That said, I once found this double-wrapping in one file in my project, committed by another developer. Tracking the changes through revision, turned out that it started as $($(this).find('selector').first()) - that is, the result of some selector was wrapped to create a new object. Then for whatever reasons, the selector was removed and only the double-wrapping of this remained. Needless to say, on the next commit it was changed to $(this).

6

As explained before me, $($(this)) and $(this) are absolutely identical. jQuery returns the same jQuery object if you try to wrap it more than once.

Additionally, for performance considerations it is a good practice to reuse jQuery objects - it is quite expensive to create jQuery objects, especially the ones with complex selectors. Example:

var $this = $(this);
if ($this.hasClass("footer_default")) {
    $('#abc')
        .appendTo($this)
        .toolbar({position: "fixed"});
}

Just google for 'jQuery best practices' - it will take a 30 min for you to learn these basics and you will use jQuery way more effectively.

3

There is no meainig of doing that.

The following code return the same:

console.log($($(this)).hasClass("footer_default"))
console.log($(this).hasClass("footer_default"))

a boolean value depenging on if the selected element has or not the class footer_default:

.hasClass( className )Returns: Boolean

Demo: http://jsfiddle.net/IrvinDominin/aSzFn/

3

$(this) and $($(this)) both return jquery object.

There is no difference between these two.

Not the answer you're looking for? Browse other questions tagged or ask your own question.