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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:33:40+00:00 2026-06-18T05:33:40+00:00

When you invoke a top-level function in Javascript, the this keyword inside the function

  • 0

When you invoke a top-level function in Javascript, the this keyword inside the function refers to the default object (window if in a browser). My understanding is that it’s a special case of invoking the function as method, because by default it is invoked on window (as explained in John Resig’s book, Secrets of the JavaScript Ninja, page 49). And indeed both invocations in the following code are identical.

function func() {
  return this;
}

// invoke as a top-level function
console.log(func() === window); // true

// invoke as a method of window
console.log(window.func() === window); // true

So far so good… Now here is the part I don’t understand:

When a function is nested in another function and invoked without specifying an object to invoke on, the this keyword inside the function also refers to window. But the inner function cannot be invoked on window (see code below).

function outerFunc() {
  function innerFunc() {
    return this;
  }

  // invoke without window.* - OK
  console.log(innerFunc() === window); // true

  // invoke on window
  //window.innerFunc(); - error (window has no such method)
  console.log(window.innerFunc) // undefined
}

outerFunc();

It makes perfect sense that the nested function isn’t available on window, as it is after all nested… But then I don’t understand why the this keyword refers to window, as if the function was invoked on window. What am I missing here?

EDIT

Here is a summary of the great answers below and some of my follow up research.

  • It is incorrect to say that invoking a function “normally” is the same as invoking it as a method of window. This is only correct if the function is defined globally.

  • The function context (the value of the this keyword) does not depend on where / how the function is defined, but on how it is being invoked.

  • Assuming that the code is not running in in strict mode, Invoking a function “normally” will have the function context set to to window (when running in a browser, or the corresponding global object in other environments).

  • An exception to the above rules is the use of bind to create a function. In this case even if the function is invoked “normally”, it could have a context other than window. That is, in this case the context is determined by how you create the function, rather than how you invoke it. Although strictly speaking this isn’t accurate, because bind creates a new function that internally invokes the given function using apply. The context of that new function will still be determined by the way it’s invoked, but it shields the context of the function it internally invokes by using apply.

By invoking “normally” I refer to the following simple way of invocation:

myFunction();

To complete the picture, here is a brief coverage of other ways of invocation and the corresponding context:

  • As a property of an object (method) – the context is the object

  • Using apply or call – the context is specified explicitly

  • With the new operator (as a constructor) – the context is a newly created object

Feel free to update the above as necessary, for the benefit of people with similar questions. Thanks!

  • 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-18T05:33:41+00:00Added an answer on June 18, 2026 at 5:33 am

    You can call any function that is in scope with functionName(). Since you haven’t called it on an object, it will be called in the context of the default object (window). (IIRC, it will be called in the context of undefined if you are in strict mode).

    The default object for context has nothing to do with where a function is defined or what scope that function appears in. It is simply the default object.

    If a function is a property of an object, you can call it as reference.to.object.function(), and it will be called in the context of object instead of the default object.

    Other things that change the context are the new keyword and the apply, call, and bind methods.

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

Sidebar

Related Questions

How can I invoke a top-level function by name? For example, #!/usr/bin/env python import
From version to version of .NET the more function that's equal to P/Invoke is
this.Invoke((MethodInvoker)delegate { code; }); This results in exception as Cannot access a disposed object.
I'm attempting to hook a top level standard library module. That is, provide an
How can I efficiently tell if the mouse is over a top-level window? By
I'm trying to invoke a method from another class that means I want to
I want to invoke a selector of a method that has the usual NSError**
I am using GNU make , where I have a top level makefile ,
I executed the function: rake db:schema:dump and that's whats I got: C:\Users\Oded\sites\simple_cms>rake db:schema:dump (in
When running the top level maven target test I get the following error: FATAL:

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.