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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:26:43+00:00 2026-06-16T11:26:43+00:00

I’m fairly new to JavaScript and I’m cleaning up some JavaScript code I downloaded.

  • 0

I’m fairly new to JavaScript and I’m cleaning up some JavaScript code I downloaded. One of the inconsistencies in this code is mixed usage of both this and the local variable that that references it.

An example code snippet (private method within a jQuery UI widget):

_populateLists: function(options) {
    //do stuff with this

    var that = this;
    options.each(function(index) {
        //use both this AND that, they are different in this scope
    });

    //here this and that are the same thing
    //I want to be consistent, is one preferable over the other?
},

In many places throughout the code, where the scope is such that this === that, there is mixed usage, even within the same line of code.

For the sake of readability and maintainability, is it preferable to use this or that?

Note: I realize a lot of these types of things depend on developer preference. But before I decide to rewrite the code to use one over the other, I’d like to understand any reasoning if/why one would be preferred over the other.

EDIT: In this script, I think the only reason this is being assigned to a local variable is so that it can be referred to from within the inner closure.

  • 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-16T11:26:46+00:00Added an answer on June 16, 2026 at 11:26 am

    The reason that the value of this is commonly assigned to a local variable is so that you can close over that value and use it inside a nested function.

    this is a special variable and somewhat different from normal local variables in that it is automatically set to the object upon which a function was called (if any); otherwise, the global object. However, this intrinsic value of this is somewhat muddied by jQuery’s liberal use of call and apply, which allow the caller to specify the value of this.

    In a nested function, this does not inherit the value of its parent’s this in the same way it would inherit a parent’s local variables through the scope chain.

    Because of this, we have to stash the value of this in a local variable if we need it inside a nested function, such as the each callback in your example.

    var a = { fn: function() {
        // here, `this` is the object `a`
        var x = this;
        function b() {
            // here, `this` is `window` because `b` is invoked with no context
            // ...but we still have access to `x`, whose value would be `a`
        }
        b();
    }};
    
    a.fn(); // by invoking `fn` as a property of `a`, we implicitly set `fn`'s
            // `this` to `a`.
    
    // Compare to:
    var f = a.fn;
    f();    // we now invoke `fn` with no context, so its `this` will be `window`.
    a.fn.call(window); // or by specifying context explicitly
    

    Of course, when you’re still in the parent scope, this will still equal that (or self or whatever). At first glance, it may seem like there’s no difference between the two, but there is one important performance impact:

    Minification. If you assign this to a local variable, all references to that variable can be reduced to one character. References to this cannot. Compare this minified output:

    function a() {this.w=1;this.x=2;this.y=3;this.z=4;}
    function b() {var t=this;t.w=1;t.x=2;t.y=3;t.z=4;}
    

    With 4 references to this, you save bytes by using a variable. If you have to capture this for an inner function anyway, using the local variable instead of this in the outer function gets you savings even with a single reference.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,

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.