Is this line
$(this).attr("id").replace("_button","");
equivalent to this one?
this.attr("id").replace("_button","");
In JavaScript, the this keyword refers to an object. Which object depends on how this is being invoked (used or called). The this keyword refers to different objects depending on how it is used: In an object method, this refers to the object.
The this keyword refers to the current object in a method or constructor. The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same name (because a class attribute is shadowed by a method or constructor parameter).
In regular functions the this keyword represented the object that called the function, which could be the window, the document, a button or whatever. With arrow functions the this keyword always represents the object that defined the arrow function.
A function is called by writing its name, followed by (). If the function takes arguments, the arguments are passed, in the order listed in the function declaration, in the parentheses.
since this
always contains a reference to the object of invocation
, it really depends where you call that code.
If you call this within a jQuery event handler
, this
is a reference to the DOM element
itself, so you need to translate it into a jQuery object by calling $()
before you can call jQuery methods on it. That in turn means
this.attr("id").replace("_button","");
will not work there.
If you're writting a plugin method for instance, this
already IS a jQuery object
(reference
) and both lines would actually do the same. Of course, if this
already is a jQuery object
you doing extra work, trying to parse it again.
example:
$.fn.yourplugin = function(){
// this refers to a jQuery object
return this.each(function(i,v){
});
});
It depends on the context, but probably not.
If you have an event handler, say:
$('#_button_foo').click(function() {
$(this).attr("id").replace("_button","");
});
Then this
will refer to the DOM element whereas $(this)
creates a jQuery object and let you call jQuery functions on this object.
On the other side, if you develop a plugin and have
jQuery.fn.plugin = function() {
this.each(function() {...});
}
then this
refers to the jQuery object you call the method on.
this
refers to the object it is used in. In the global scope it refers to window
and in a method call it refers to the object the method is called on.
In your case this
will probably refer to an element in the DOM that is fetched with some other jQuery mechanism like an event (click
, hover
, etc.) or an iteration over a list of elements (like jQuery.each
). In that case you only get that DOM element but not a jQuery object with the extension of jQuery methods like attr
. So you need to turn it into an jQuery object using the $
constructor function.
The $(this) will turn the DOM Object into a jQuery object, so you can use the jQuery functions.
So $(this)
and this
are not the same.
In Javascript, the keyword 'this
' refers to the object whose context you're in. So if you're calling myObj.method1()
, then inside method1's code, 'this
' would refer to myObj
.
In jQuery, most operations return a jQuery result set object, often called a jQuery wrapper. For example, the selector $("a")
would return a jQuery result set that contains all the <a>
DOM elements. $("a").get(0)
would return the first anchor DOM element, $("a").get(1)
would return the second anchor, etc.
So $(this)
refers to the jQuery wrapper object, and this
refers to the DOM element.
So to sum it all up: $(this).get(0)
would be the same as this
.
It depends on which context you are using it.
$(this)
is a jQuery object and this
is a DOM object.
So, they are not same.
If you are developing jquery plugin then this
is same as $(this)
.
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