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 3947986
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:19:15+00:00 2026-05-20T01:19:15+00:00

From docs I understand that .proxy() would change the scope of the function passed

  • 0

From docs I understand that .proxy() would change the scope of the function passed as an argument. Could someone please explain me this better? Why should we do this?

  • 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-05-20T01:19:16+00:00Added an answer on May 20, 2026 at 1:19 am

    What it ultimately does is it ensures that the value of this in a function will be the value you desire.

    A common example is in a setTimeout that takes place inside a click handler.

    Take this:

    $('#myElement').click(function() {
            // In this function, "this" is our DOM element.
        $(this).addClass('aNewClass');
    });
    

    The intention is simple enough. When myElement is clicked, it should get the class aNewClass. Inside the handler this represents the element that was clicked.

    But what if we wanted a short delay before adding the class? We might use a setTimeout to accomplish it, but the trouble is that whatever function we give to setTimeout, the value of this inside that function will be window instead of our element.

    $('#myElement').click(function() {
        setTimeout(function() {
              // Problem! In this function "this" is not our element!
            $(this).addClass('aNewClass');
        }, 1000);
    });
    

    So what we can do instead, is to call $.proxy(), sending it the function and the value we want to assign to this, and it will return a function that will retain that value.

    $('#myElement').click(function() {
       // ------------------v--------give $.proxy our function,
        setTimeout($.proxy(function() {
            $(this).addClass('aNewClass');  // Now "this" is again our element
        }, this), 1000);
       // ---^--------------and tell it that we want our DOM element to be the
       //                      value of "this" in the function
    });
    

    So after we gave $.proxy() the function, and the value we want for this, it returned a function that will ensure that this is properly set.

    How does it do it? It just returns an anonymous function that calls our function using the .apply() method, which lets it explicitly set the value of this.

    A simplified look at the function that is returned may look like:

    function() {
        // v--------func is the function we gave to $.proxy
        func.apply( ctx );
        // ----------^------ ctx is the value we wanted for "this" (our DOM element)
    }
    

    So this anonymous function is given to setTimeout, and all it does is execute our original function with the proper this context.

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

Sidebar

Related Questions

From the isset() docs : isset() will return FALSE if testing a variable that
I'm speaking of this module: http://docs.python.org/library/operator.html From the article: The operator module exports a
From what information I could find, they both solve the same problems - more
Is there a reliable way to extract text from PDF? The first thought that
From a web developer point of view, what changes are expected in the development
From a desktop application developer point of view, is there any difference between developing
From what I've read, VS 2008 SP1 and Team Foundation Server SP1 packages are
From time to time I see an enum like the following: [Flags] public enum
From the Immediate Window in Visual Studio: > Path.Combine(@C:\x, y) C:\\x\\y > Path.Combine(@C:\x, @\y)
From time to time I get a System.Threading.ThreadStateException when attempting to restart a thread.

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.