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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:41:43+00:00 2026-06-10T06:41:43+00:00

I am trying to understand why an inner function can access a public property

  • 0

I am trying to understand why an inner function can access a public property of an outer function when the outer function is called directly, but not when it is assigned to a variable?

Example:

function outer(x,y){

    this.x = x;
    this.y = y;

    function inner(){
        alert(this.x);       
    }

    inner();
}

outer(1,2); //As expected, alerts 1
var func = outer(1,2) //Also alert 1
var func2 = new outer(1,2); //Alerts undefined

One thing I tried was to remove the this keyword from alert(this.x); and it did work for all three cases. However, if I do remove the this keyword, I’d be accessing the passed in param, not the public variable, which is definitely not the desired action. Can someone explain this behavior?

  • 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-10T06:41:44+00:00Added an answer on June 10, 2026 at 6:41 am

    When you call outer(1, 2) like that, this is a reference to window, so “x” and “y” are effectively global variables. That’s why inner() can access “x”.

    When you call new outer(1, 2) you have caused this (in “outer”) to be a reference to a new object. When “inner” is called inside “outer”, this will still reference window, so there’s no “x”.

    The value of this is determined for every function call, and the value depends only on the particulars of that call. Thus the fact that you call “outer” via new has no effect on the interior call to “inner” — because you simply call the function as inner();, the value of this inside that function will be a reference to window (well, the global context, whatever that is).

    Here are the ways this can be set upon a call to a function:

    1. If the function is called via the new operator, then this will refer to a newly-created object.
    2. If the reference to the function is obtained via a property lookup on an object (foo.someFunction()), then this will be a reference to that object.
    3. If the function is called via .call() or .apply() from the Function prototype, then this will refer to the first argument to whichever of those functions was used, coerced to an object value if necessary.
    4. If the function is called via a simple “naked” reference, then this will refer to the global context (window in a browser). edit — Šime Vidas points out in a comment above that in strict mode, this case results in this being null (which really makes a little more sense, and would avoid the weirdness observed in the OP).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand the following example, that is similar (but not equal)
I'm not really that good with regex, but I understand the basics. I'm trying
I am trying to understand the inner workings of queue (3) macros in Freebsd.
Trying to understand the options for will_paginate's paginate method: :page — REQUIRED, but defaults
I'm trying to understand why the following is failing and I'm not able to
I am trying to understand why this query in MySQL does not return any
I am trying to understand what the STL time series function does and in
I am trying understand ViewModels deeper and I have read many articles and blogs
Trying to understand what's the correct way of implementing OpenID authentication with Spring Security.
Trying to understand the Deezer API. When I visit: http://connect.deezer.com/oauth/auth.php?app_id=MY_APP_ID&redirect_uri=http://mydomain.me&perms=basic_access I end up at

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.