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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:27:01+00:00 2026-05-13T13:27:01+00:00

Basically I am trying to understand and learn the ‘this’ keyword’s working principle in

  • 0

Basically I am trying to understand and learn the ‘this’ keyword’s working principle in JavaScript.

As far as I understand ‘this’ refers to the object (function) that it is inside at that moment.

So, by believing this, I wanted to test the output of the simple code below:

<body>

<input type="button" value="Add Age" onclick="Outer()" />

<script type="text/javascript">

function Outer(){

if(typeof this.Father == 'undefined')
    {
        this.Father = 0; 
    }

this.Father+=2;
alert(this.Father);

inner();

        function inner(){
            if(typeof this.Son== 'undefined')
            {
                this.Son = 0;
            };

            this.Son++;
            alert(this.Son);
            alert(this.Father);
        };
};
</script>
</body>

And its output confuses me. Because in the inner() function, this.Son outputs the incremented integer value of the Son. But I expect this.Father to fail because inner() does not have a .Father attribute. But instead of throwing an exception it alerts the value of this.Father -Which seems

  • a line above ‘this’ refers inner()
  • and following line ‘this’ refers Outer()

At this point i have 2 questions in my mind actualy:

  1. Does ‘this’ keyword refers always to
    the outer scope’s housing even inside the inner functions?

  2. And without having any instances declared ‘this’ keyword references what in the method? ( I mean without having something lik var myFamily = new Outer() )

Thanks,

burak ozdogan

  • 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-13T13:27:01+00:00Added an answer on May 13, 2026 at 1:27 pm

    this is determined by the invocation pattern, that is, how a function object is being called.

    There are 4 different kinds of invocation patterns:

    1. method invocation: function are defined as a property of some object, and is called via the object using refinement, that is, ..

      a.func(); // this refers to the object, i.e., a.

    2. function invocation: plain function call.

      func(); // this is bond to the global object.

    3. constuctor invocation: well, it is a little complicated. Since constructors are used as, well, the consturctor method for the new function objects being new, this refers to the new function object being created.

      var func = new Func(); // this refers to func while in Func constructor)

    4. apply invocation:

      func.apply(thisArg, argArray); // this is bond to the first argument

    In another words, the this in your example all refers to the global object (your onClick call Other()). You should try using new Other() instead.

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

Sidebar

Related Questions

I'm basically trying to create a generic object in javascript that I can use
I am trying to understand dynamic linq and expression trees. Very basically trying to
I am basically trying to trigger a function if the footer is inside the
I'm basically trying to convert a Unix timestamp (the time() function) to a relative
I'm basically trying to do a dropdown jquery plugin. HTML <script type='text/javascript' src='script.js'></script> <div
I am basically trying to create a display object transformation manager which will allow
Details below but basically I'm trying to understand what on my Droid is throwing
I'm basically trying to call mysqli::bind_param via call_user_func_array . Here's my function: function Insert($table,
I'm trying to understand someone's CSS >> HERE << . Basically, he has defined
I'm trying to understand lists in prolog when I stumbpled over this problem: there's

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.