Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7779559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:40:08+00:00 2026-06-01T18:40:08+00:00

These are taken from the tuts-premium Jquery vid tutorials. http://tutsplus.com/lesson/the-this-keyword/ Jeff explains what ‘this’

  • 0

These are taken from the tuts-premium Jquery vid tutorials.
http://tutsplus.com/lesson/the-this-keyword/
Jeff explains what ‘this’ is referring to each time but I’m not sure I’ve grasped the reasoning behind them all.

E.g. 1

function doSomething(e) { 
e.preventDefault(); 
console.log(this);
} 

$('a').on('click', doSomething);

In this case “this refers to the ‘a’ element” (being in this case the parent object)

I guess that’s because here the statement equates to :

$('a').on('click', function (e) { 
    e.preventDefault(); 
    console.log(this);
    } 

So ‘a’ is the parent object

E.g. 2

var obj = { 
    doIt: function(e){ 
    e.preventDefault(); 
    console.log(this);
    }
}

$('a').on('click',  obj.doIt);

In this case “this still refers to the ‘a’ element ” (*but apparently it’s not the parent object?)

It seems this time we’re calling a method but the statement still equates to the same thing as E.g. 1

*One thing in the tutorial has me a bit confused. I thought ‘this’ always refers to the parent object so in this case ‘a’ is still the parent object. But (at 05.23 in the tutorial ) he infers that’s not the case, stating “there may be times when you want ‘this’ to refer to it’s parent object which would be ‘obj’ ” in which event he creates e.g.3.

E.g. 3

var obj = { 
    doIt: function(){ 
    console.log(this);
    }
}

$('a').on('click',  function(e){    
obj.doIt(); 
        };
e.preventDefault(); 

In this case “this refers to the obj object”

I presume this to with the fact that ‘this’ is in a nested function as this statement equates to :

$('a').on('click',  function(){    
function(){ console.log(this);}
};
e.preventDefault(); 

I don’t really understand why though, particularly as I read in an article that in nested functions ‘this’ “loses its way and refers to the head object (window object)”.

E.g.4

var obj = { 
    doIt: function(){ 
    console.log(this);     
    }
}

$('a').on('click',  function(e){    
       obj.doIt.call(this);             
       e.preventDefault(); 
});

In this case “This refers to the ‘a'”

According to Javascript Definitive Guide “The first argument to both call() is the object on which the function is to be invoked”
Here “this” is the used as the first argument. But “this” is not the object on which the function is to be invoked??
I think I get that the call function is there to invoke the function and use its first parameter as a pointer to a different object but I don’t get why using ‘this’ means the function is invoked by ‘a’. It’s not something I’ve seen in other call() examples either.

Sorry for such a mammoth post. Hopefully someone’s still reading by this stage…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T18:40:10+00:00Added an answer on June 1, 2026 at 6:40 pm

    I hope this helps clarifying the issue, it can be confusing indeed.

    • When this is loose on your code, it will refer to the global object (in web browsers, that is window).

      console.log(this); // window
      
    • When this is inside an object method (like on your E.g. 3), it will refer to the object. This aplies to objects instanced with new, or as object literals (like on your example).

       var obj = { 
           doIt: function(e){ 
               console.log(this);
           }
       }
       obj.doIt(); // obj
      
    • Inside an event handler, this will refer to the object the event is bound to.

      // This is the plain js equivalent of your jQuery example
      document.getElementsByTagName['a'][0].addEventListener('click', function(e){
          console.log(this); // the first anchor on the document
      });
      
      // This is exactly the same:
      var clickHandler = function(e){
          console.log(this); // the first anchor on the document
      };
      document.getElementsByTagName['a'][0].addEventListener('click', clickHandler); 
      
      // Even if the handler is defined inside of another object, this will be
      // the obj the event is bound to. It's the case of your E.g. 2
      var obj = { 
          doIt: function(e){  
              console.log(this); // the first anchor on the document
          }
      }
      document.getElementsByTagName['a'][0].addEventListener('click', obj.doIt);
      // When you pass obj.doIt to addEventListener above, you are passing a reference
      // to that function. It's like "stealing" the function from the object
      
    • When an object is passed as the first parameter to Function.call or Function.apply, if this appears inside of the function it will refer to the object you passed. It’s a way to force what this will be pointing to.

      var obj = { 
          doIt: function(){ 
              console.log(this); // window 
          }
      }
      obj.doIt.call(window);
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is what I learned by doing some searching. These things were taken from
I have code similar to the snippet below (taken from http://leastprivilege.com/2009/05/24/use-geneva-session-management-for-your-own-needs/ ) public class
Can someone explain the difference between these two, the first one is taken from
How to I define the equivalent of this function (taken from learnyouahaskell ) inside
This is taken from Exercise 19 of The Pragmatic Programmer. A quick reality check.
I have this replace regex (it's taken from the phpbb source code). $match =
When I say -Xmx=1024m , does this include permgen i.e -XX:MaxPermSize= is taken from
This piece of code has been taken from a game built with XNA framework.
This may well have come up before but the following code is taken from
I'm writing this question because one little invisible problem has taken from me hours

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.