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

  • Home
  • SEARCH
  • 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 8594125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:07:57+00:00 2026-06-12T00:07:57+00:00

I haven’t used Javascript in a long time and have been refreshing myself on

  • 0

I haven’t used Javascript in a long time and have been refreshing myself on it today. One thing that always got me was the this keyword. I know in jQuery event handlers, such as the click event, this refers to the element that fired the event. How is this passed to the function that I give it as a callback even though my function has no arguments?

Given the following code:

$("tr.SummaryTbRow").data("Animating", false);
$("tr.SummaryTbAltRow").data("Animating", false);

$("tr.SummaryTbRow").click(function () {
    if ($(this).data("Animating") == false) {
        if ($(this).next(".Graph").css("display") == "none") {
            $(this).data("Animating", true);

            //Part I am questioning.
            setTimeout(function () {
                $(this).data("Animating", false);
            }(this), 550);

            $(this).next(".Graph").slideRow('down', 500);
        }
        else {
            $(this).data("Animating", true);
            $(this).next(".Graph").slideRow('up', 500);
        }
    }
});

I am trying to figure out how to pass the element table row with class SummaryTbRow to my setTimeout call back function. Does jQuery pass this in a similar fasion to what I am doing with my anonymous call back function? Does my this inside the function refer to the this I pass in?

I know I could just do:

setTimeout(function (element) {
    $(element).data("Animating", false);
}(this), 550);

But I want to figure out how jQuery is able to pass this to my call back function even though my function takes 0 arguments.

  • 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-12T00:07:58+00:00Added an answer on June 12, 2026 at 12:07 am

    Short answer:

    You can set this on a function by using the function’s .call() and .apply() methods.

    Long answer:

    The this variable on any function is similar to the arguments variable (which is probably something you didn’t know about). It’s set when the function is called and is an artifact of how the function is called. To explain, let me start with a demonstration of arguments. Consider:

    myFunction = function () {
        return arguments.length;
    };
    

    Now let’s look at a couple calls to myFunction:

    myFunction(); //is 0
    myFunction(null); //is 1
    myFunction(undefined); //is 1
    myFunction(0, 0, 0, 0, 0); //is 5
    

    As you can see, the value of arguments.length is not dependent on how or where we wrote the function, but on how we called the function. The same is true of the this variable (otherwise known as the “calling object”). There are exactly 3 methods for setting the calling object (there’s a sort-of 4th in ES5, but we’ll ignore that):

    1. You can set it by calling the function using dot-notation (e.g. something.myFunction())
    2. You can set it by using the function’s .call() or .apply() methods (e.g. myFunction.call(someObject))
    3. If it’s not set using method #1 or #2, it will default to the global object (e.g. window)

    So most people are used to method #1. If you assign your function as a property of an object, then call the function using the object and dot-notation, then the object gets set as this. Like so:

    var myFn = (function () { return this.x });
    
    var myObj = {
        x: 1,
        y: myFn
    };
    
    myObj.myFn(); //is 1
    

    But we can also use method 2 if myFn isn’t a property of the object we want to call it on but the object follows the correct form for myFn to be able to act on it (see: duck typing):

    var myOtherObj = {
        x: 2
    }
    
    myFn.call(myOtherObj); //is 2
    myFn.apply(myOtherObj); //is 2
    myFn.apply({ x : 3}); //is 3
    

    Pretty cool, eh? This is how jQuery does it. When they execute your callback, they do so using .apply(event.target) (setting this to the event’s target object). They do it in a more complex manner because of their callbacks framework, but the idea is there.

    Anyway, I wouldn’t be offering a long answer if I didn’t toss in an explanation of method #3, which drives some people totally insane: What happens if you don’t set the calling object?

    Because all global variables are implicit properties of the global object, you get some interesting effects from method #3. Such as:

    var x = 4;
    myFn(); //is 4
    

    But most of the time you’re not lucky enough to have your global object meet the requirements the function has for its calling object, so usually it just results in an error and a lot of frustration.

    Probably more than you were looking for, but hopefully you’re now much more informed on the calling object and its wily ways.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Haven't been programming in JS for a while. Now, I have following thing: <html>
I haven't worked with SQL Reporting much, however I have been trying to get
Haven't seen anything, but I have seen that you can create albums in the
haven't used regex replaces much and am not sure if how I have done
Haven't used C++ in a while. I've been depending on my Java compiler to
Haven't seen this feature anywhere else. I know that the 32nd bit is used
Haven't done as much of Javascript as I should have done. I am trying
Haven't been able to find this one out. How are Bitmaps stored in memory
I haven't used the implements keyword before, and I've been trying to use it
Haven't seen anything about it here but it seems to solve one of the

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.