Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: $(this) vs. this.$()

In Ember.js's docs, they have a jQuery code snippet with the following syntax:

this.$().button();

Is this snippet only turning this into a jQuery object so that the jQuery UI .button() function can be called on it?

Would this snippet be identical?

$(this).button();
like image 960
Noah Freitas Avatar asked Jun 18 '12 21:06

Noah Freitas


People also ask

When we should use $( this in jQuery?

The value of this inside a click event is a DOM element (the one that was clicked). Using $(this) converts it to a jQuery object. DOM elements do not have a hide() methods, but jQuery adds that and many other methods you can then call.

What does $( this mean in jQuery?

$(this) is a jQuery wrapper around that element that enables usage of jQuery methods. jQuery calls the callback using apply() to bind this . Calling jQuery a second time (which is a mistake) on the result of $(this) returns an new jQuery object based on the same selector as the first one.

Is vanilla JavaScript faster than jQuery?

It is said that jQuery is better for DOM manipulation than Javascript, however, after monitoring both of their performances, vanilla JS was found to be faster than jQuery.


1 Answers

The source code explains this as follows:

/**
    Returns a jQuery object for this view's element. If you pass in a selector
    string, this method will return a jQuery object, using the current element
    as its buffer.

    For example, calling `view.$('li')` will return a jQuery object containing
    all of the `li` elements inside the DOM element of this view.

    @param {String} [selector] a jQuery-compatible selector string
    @returns {Ember.CoreQuery} the CoreQuery object for the DOM node
  */
  $: function(sel) {
    return this.invokeForState('$', sel);
  },

So to answer your question: no it's not the same as $(this), which would wrap the ember view instance in a jQuery object...

like image 176
Esailija Avatar answered Sep 28 '22 03:09

Esailija