Understanding jQuery's 'this' Keyword

  • Thread starter Thread starter jeff1evesque
  • Start date Start date
Click For Summary
The discussion centers on the use of the keyword 'this' in jQuery, highlighting its dual context as a DOM object within callback functions and as a jQuery object in custom functions. The user expresses confusion about the necessity of 'this' in certain code segments, particularly questioning its redundancy in function calls and whether it can be replaced with direct element references. They note that while 'this' can often be substituted with the object name (like holder.Slideshow), it serves a purpose in defining properties like 'this.Counter = 1' to ensure they are associated with the correct context. The user concludes that while they believe 'this' may be stylistically unnecessary in some cases, they seek clarification on its importance in variable declarations and overall functionality in jQuery. The discussion also references a helpful resource for understanding the 'this' keyword in JavaScript.
jeff1evesque
Messages
312
Reaction score
0
I've read in several sources that the keyword 'this', and have come up with the following summary:

There are two "main contexts" where the keyword 'this' is used within
jQuery. The first refers to a DOM object, and the second to a jQuery
object. 'this' is a DOM element when we are inside of a callback function
(in the context of jQuery for example, being called by the click, each,
bind, etc. methods). 'this' is a jQuery object when we are inside own
jQuery functions.

But I am having difficulty with the context it is used, and having a difficult time trying to understand it's equivalent. I've also attached the HTML file (if necessary, and if people have the time to read)

-------------------------------------------------------------------------------------
This is a .js segment of code I would like to look at (for those that also have the time):
-------------------------------------------------------------------------------------

//jQuery allows for the web-page to behave dynamically.

var holder = $.fn;
holder.extend({
SplitID : function()
{
return this.attr('id').split('-').pop();​
},​

Slideshow : {
Ready : function()
{
$('div.tmpSlideshowControl')
.hover(
function() {
$(this).addClass('tmpSlideshowControlOn');​
},​
function() {
$(this).removeClass('tmpSlideshowControlOn');​
}​
)​
.click(
function() {
holder.Slideshow.Interrupted = true;

$('div.tmpSlide').hide();
$('div.tmpSlideshowControl').removeClass('tmpSlideshowControlActive');

$('div#tmpSlide-' + $(this).SplitID()).show()
$(this).addClass('tmpSlideshowControlActive');​
}​
);​

this.Counter = 1[/color];
this.Interrupted = false;

--------------------------------------------------------------------------------------
My Question:

1.)
Every keyword 'this' could be removed from the segment of code above, except for 'this.Counter = 1'. But for the rest of the code why not just remove it? For instance "$(this).whatever." could simply just be written as "$.whatever".

2.)
Ready() is not a callback function, so the keyword 'this' used in this segment refers to a jQuery element. Instead of writing 'this', could we write the element's name in its place? For instance $(element's name).removeClass('tmpSlideshowControlOn'); (elements name for a slide show would be the ID name defined in the HTML?)?​

3.)
Why is the keyword 'this' is required in this.Counter = 1? Is it because 'this' refers to a DOM element, or because Counter is a new variable, I am pretty confused? When I wrote this.Counter=1 as holder.Slideshow.Counter = 1, the page loaded the same- from which I concluded both are the same. But the question remains, why is this required here, and other portions (above that particular line) of the code, I could remove the keyword 'this'?​

Any help would be great, thanks.​
 

Attachments

Last edited:
Technology news on Phys.org
Question #3:
I think the answer to #3 is because Counter was not previously defined, so we had to define it within the jquery object Slideshow. Otherwise the variable Counter would be syntaxically incorrect.

Question #2 (and thus #1):
All the references to the keyword 'this' could be replaced with holder.Slideshow (or could be completely removed, unless a new variable is being declared), which leads me to conclude that the keyword 'this' is something of a convention of style (in my opinion useless). Thus, I feel that I've answered my own questions, but if I'm wrong in anyway, could someone be kind enough to let me know.

Thanks,

JL
 
Last edited:
'this' is a keyword in Javascript (and Java) and is not specific to JQuery. This site should help with what you're trying to understand: http://www.quirksmode.org/js/this.html"
 
Last edited by a moderator:
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
5
Views
15K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 6 ·
Replies
6
Views
5K