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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:56:01+00:00 2026-05-29T07:56:01+00:00

Scope or visibility of the this pointer which is called w.r.t a div is

  • 0

Scope or visibility of the this pointer which is called w.r.t a div is restricted to the function to which it was intended to and not to its NESTED functions.

But,all the local variables of that function are visible in the nested functions — why does this happen??.. getting the this pointer via calling the function with <function name>(this) on a div — doesnt this mean even the this pointed also is a local variable in the <function name>

In short, here’s the code which confused me..

<html>
<script type="text/javascript" >
    function tst1(){
    setTimeout(
    function (){
    alert(this.id)
    }, 2000)
    }
    function tst2(){
    var elem = this
    setTimeout(
    function (){
    alert(elem.id)
    }, 2000)
    }
</script>
<style type="text/css">
    .test{
    margin: 40px 100px;
    background-color: green;
    width: 200px;
    line-height: 40px;
    text-align: center;
    height: 40px;
    }
</style>
<div id="fooBar"  class="test" onclick="tst1.call(this)"> Emily Rossum</div>
<div id="chickenRun" class="test" onclick="tst2.call(this)"> Emily Rossum</div>
</html>

In the tst1 function the this variable is null but the local variable of that function should be visible in the nested functions as shown in tst2.

I was reading on closures, since the function is being called later — via setTimeout even after the tst1 and tst2 are returned — aren’t both the functions closures? (If so,this being a variable in the parent function should be visible in the nested function, shouldn’t 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-29T07:56:04+00:00Added an answer on May 29, 2026 at 7:56 am

    The this variable in javascript is different from other variables in that it’s always specific to the particular function invocation. There is essentially one this for every function invocation and it’s not necessarily, although it may be, the same this which was used to in the outer function.

    If you want to preserve the outer this for use within the inner functions then save it to a local.

    var outer = function () { 
      var self = this;  // Save current 'this' in a local
      var inner = function () { 
        self.name;  // refers to the original outer 'this.name' 
        ...
      };
      ...
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Global scope allows you to use a variable in a function that was defined
The scope of this is that we have three main projects. Some of the
scope issue in PHP classes: Why does this work? class index extends Application {
I want to toggle visibility of element in {{#with}} scope, depending on the value
The scope of #define is till the end of the file. But where does
What is the scope of $1 through $9 in Perl? For instance, in this
I like to scope all of my JQuery functions and event sinks to $(window).load,
When I provision a scope for synchronization in an SQL 2008 database like this:
I'd like to know what the scope and visibility of TempData is in ASP.NET
SQL Server books online states the following about visibility (scope?) of temporary tables: Temporary

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.