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

The Archive Base Latest Questions

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

In a JavaScript file I saw: function Somefunction(){ var that = this; … }

  • 0

In a JavaScript file I saw:

function Somefunction(){
   var that = this; 
   ... 
}

What is the purpose of declaring that and assigning this this to it?

  • 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-19T16:15:47+00:00Added an answer on May 19, 2026 at 4:15 pm

    I’m going to begin this answer with an illustration:

    var colours = ['red', 'green', 'blue'];
    document.getElementById('element').addEventListener('click', function() {
        // this is a reference to the element clicked on
    
        var that = this;
    
        colours.forEach(function() {
            // this is undefined
            // that is a reference to the element clicked on
        });
    });
    

    My answer originally demonstrated this with jQuery, which is only very slightly different:

    $('#element').click(function(){
        // this is a reference to the element clicked on
    
        var that = this;
    
        $('.elements').each(function(){
            // this is a reference to the current element in the loop
            // that is still a reference to the element clicked on
        });
    });
    

    Because this frequently changes when you change the scope by calling a new function, you can’t access the original value by using it. Aliasing it to that allows you still to access the original value of this.

    Personally, I dislike the use of that as the alias. It is rarely obvious what it is referring to, especially if the functions are longer than a couple of lines. I always use a more descriptive alias. In my examples above, I’d probably use clickedEl.

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

Sidebar

Related Questions

I just saw this piece of javascript code at Facebook: javascript:eval(function(p,a,c,k,e,r){e=function(c){return c.toString(a)};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return
I have a javascript file that reads another file which may contain javascript fragments
How can I load an external JavaScript file using a bookmarklet? This would overcome
I have some code in a javascript file that needs to send queries back
How can you reliably and dynamically load a JavaScript file? This will can be
$('#selector').click(function() { // here I would like to load a javascript file // let's
xml.etree.ElementTree.parse is choking on my xhtml file. I saw somewhere that lxml can handle
Say I have a fairly hefty JavaScript file, packed down to roughly 100kb or
I want to create a compiled JavaScript file for my website. For development I
Say I need to call a javascript file in the <head> of an ERb

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.