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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:10:32+00:00 2026-06-11T10:10:32+00:00

The this keyword always refers to the object that the containing function is a

  • 0

“The this keyword always refers to the object that the containing function is a method of.”

Great, sounds simple enough, but here’s what I’m wondering about…

For example:

function func1() {

  function func2() {
    alert(this == window); // true
  }

  func2();

  alert(this == window); // true
}

func1.func3 = function () {

  alert(this == window); // false
  alert(this == func1); // true

};

func1();
func1.func3();

Now, since func1 is actually a method of the global (window) object (a function object assigned to the property func1 of the global object) it makes sense that this inside func1 refers to the global object, and since func3 is a method of func1‘s function object it makes sense that this inside func3 refers to func1‘s function object.

The thing that bothers me is func2. I know that this inside a nested function is also supposed to reference the global object, but I’m not sure why since func2 is NOT a method of the global object. As far as I understand (and this is the part I might be completely wrong about) func2 is a method of func1‘s call (activation / variable) object. Now, if I’m right about this (and I’m not sure that I am) then shouldn’t this inside func2 refer to func1‘s call object instead of the global object?

So, I guess my question would be: Is a nested function a method of the call (activation) object of the function it is nested in, and if so, shouldn’t this refer to that call object instead the global object?

  • 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-11T10:10:34+00:00Added an answer on June 11, 2026 at 10:10 am

    The this keyword always refers to the object that the containing function is a method of.

    No. Unfortunately, it is not easy as that. The documentation of the this keyword at MDN gives a good overview. It is set to the object when the function is called as a method on it, but there are other possibilies. The default is that this is undefined when it is called without anything special, like you do with func1 and func2. For sloppy (non-strict) mode functions undefined (and null) are not used though, this does point to the global object (window in browsers) for them in that case – what you are observing.

    But it could also point to fresh object instances when the function is called as a constructor (with the new keyword), or to an event target (like a DOM element) when used as a handler. Last, but not least, it could be set manually with call, apply or bind…

    this has nothing to do with nesting. Nesting function declarations/expressions only affects the scope (“privacy”, availability) of variables. While the variable scope of a function never changes, the value of this can be different on every invocation – it is more like an extra argument.

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

Sidebar

Related Questions

I know the function of this keyword, but I would like to know how
This Javascript code is using the 'this' keyword inside a nested function (which is
What does 'this' keyword refer to when used in global object? Let's say for
I want to do something like this, but the this keyword doesn't seem to
When you have an object instance in C#, you can use the this keyword
Sorry for asking it again, there are already some questions about this keyword. But
Having read some postings here about this topic, I realize that there are quite
In some cases, the this keyword may not refer to the object I expect
I want to ask why we use this keyword before the parameter in an
I am wondering whether I can use the this keyword inside a C# lambda,

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.