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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:28:06+00:00 2026-05-15T21:28:06+00:00

I was working on a project for a while, trying to figure out what

  • 0

I was working on a project for a while, trying to figure out what I was doing wrong, when I finally narrowed “the bug” down to the fact that the below code doesn’t work as I expected:

function Alpha()
    {
    this.onion = 'onion';

    function Beta()
        {
        alert(this.onion);
        }

    Beta();
    }

alpha1 = new Alpha();
// Alerts 'undefined'

However, if I change the code to this:

function Alpha()
    {
    var self = this;
    this.onion = 'onion';

    function Beta()
        {
        alert(self.onion);
        }

    Beta();
    }

alpha1 = new Alpha();
// Alerts 'onion'

it works like I would expect. After wasting a large portion of my life, can anyone explain why it works like this?

  • 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-15T21:28:07+00:00Added an answer on May 15, 2026 at 9:28 pm

    Works like this because each function has associated its own execution context.

    However there are other ways to do it, for example:

    Using call or apply to invoke the function:

    function Alpha() {
      this.onion = 'onion';
    
      function Beta() {
        alert(this.onion);
      }
    
      Beta.call(this);
    }
    
    var alpha1 = new Alpha();
    // Alerts 'onion'
    

    The new ECMAScript 5th Edition Standard, introduces a way to persist the function context, the Function.prototype.bind method:

    function Alpha() {
      this.onion = 'onion';
    
      var Beta = function () {
        alert(this.onion);
      }.bind(this);
    
      Beta();
    }
    
    var alpha1 = new Alpha();
    // Alerts 'onion'
    

    We can say that the Beta function is bound, and no matter how you invoke it, its this value will be the intact.

    This method is not widely supported yet, currently only IE9pre3 includes it, but you can include an implementation to make it work now.

    Now let me elaborate about how this works:

    The this value exist on each execution context, and for Function Code is set implicitly when a function call is made, the value is determined depending how the reference if formed.

    In your example, when you invoke Beta();, since it is not bound to any object, we can say that the reference has no base object, then, the this value will refer to the global object.

    Other case happens when you invoke a function that is bound as a property of an object, for example:

    var obj = {
      foo: function () { return this === obj;}
    };
    obj.foo(); // true
    

    As you can see, the reference being invoked obj.bar(); contains a base object, which is obj, and the this value inside the invoked function will refer to it.

    Note: The reference type is an abstract concept, defined for language implementation purposes you can see the details in the spec.

    A third case where the this value is set implicitly is when you use the new operator, it will refer to a newly created object that inherits from its constructor’s prototype:

    function Foo () {
      return this; // `this` is implicitly returned when a function is called 
    }              // with `new`, this line is included only to make it obvious
    
    var foo = new Foo();
    foo instanceof Foo; // true
    Foo.prototype.isPrototypeOf(foo); // true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.