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();
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.
$(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.
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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With