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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:17:17+00:00 2026-06-09T14:17:17+00:00

Possible Duplicate: JS: var self = this? When looking at arbitrary code written in

  • 0

Possible Duplicate:
JS: var self = this?

When looking at arbitrary code written in JavaScript (e.g. on GitHub), many developers use var self = this and then use self instead of this to reference the current object.

What is the rationale behind this approach?

  • 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-09T14:17:19+00:00Added an answer on June 9, 2026 at 2:17 pm

    The value of this is contextual. Writing var self = this is a way to save the value of this in one context so that it can be used in another.

    Example:

    function Test() {
        var self = this;
        console.log(this);
        setTimeout(function() {
            console.log(this);
            console.log(self);
        }, 1000);
    }
    

    Prints:

    Test {}
    Window 11918143#comment15869603_11918143
    Test {}
    

    Notice that the value of this has changed, but we can still refer to the original value using self.

    This works because functions in JavaScript “close over” the variables in their lexical scope (which is just a more technical way of saying that the inner function can see variables declared in the outer function). This is why we write var self = this; the variable self is available to all inner functions, even if those functions don’t execute until long after the outer function has returned.

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

Sidebar

Related Questions

Possible Duplicate: Decode obfuscated JavaScript Question: I have the following javascript code: var _0xe91d=[\x28\x35\x28\x24\x29\x7B\x24\x2E\x32\x77\x2E
Possible Duplicate: Generating random numbers in Javascript I have the following code var randomnumber=Math.floor(Math.random()*101);
Possible Duplicate: Get property of object in JavaScript var Terminal = function() { this.walk
Possible Duplicate: Javascript doesn't work in IE8 I have the following code var ind=1;
Possible Duplicate: Self-references in object literal declarations Given this object: var OBJ = (function(){
Possible Duplicate: What does var that = this; mean in javascript? I often find
Possible Duplicate: javascript replace function not working Here is my JS code: var imgTitle
Possible Duplicate: Javascript Variable Variables Please take a look at code below var selected_id=1;
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} AFAIK, there
Possible Duplicate: How do I test for an empty Javascript object from JSON? var

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.