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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:14:51+00:00 2026-05-31T00:14:51+00:00

So this is tough. When calling a method directly, this refers to the containing

  • 0

So this is tough. When calling a method directly, this refers to the containing object while when calling from string conversion, this refers to “window” (i believe, i could still be completely wrong).

Could anyone explain this to me?

What will happen is a javascript error AFTER the first alert statement (that means direct call worked fine with how _this was defined)

/**
 *  converts a string to a function
 */
function stStringToFunction(string) {
    var fnList = string.split(".");
    var currentFn = window;
    var nextFn;
    while(currentFn !== undefined && (nextFn = fnList.shift())) {
        currentFn = currentFn[nextFn];  
    } 
    return currentFn;
}

//An example library
UnderstandingThis.ui.Component1 = {
    performTask1: function(options) {
        var settings = {
                opt1: true,
                opt2: false
            },
            _this = this;

        $.extend(settings, options);

        _this._PrivateTask1(settings);
    },
    _PrivateTask1: function(settings) {
        //Some sweet stuff here!
        alert("Private task1: " + settings.from);
    }
}
UnderstandingThis.ui.Component1.performTask1({from: "direct"});
stStringToFunction("UnderstandingThis.ui.Component1.performTask1")({from: "string-to-function"});

My lack of explanation required an edit: 2/21/2012 9:56 AM (Montana)

So the problem is that the faux library (UnderstandingThis.ui.Component1) contains a function that uses _this (which is set to this) and works fine when doing a direct method call. By that i mean in javascript calling UnderstandingThis.ui.Component1.performTask1(). But if i call this same task via a string conversion stStringToFunction("UnderstandingThis.ui.Component1.performTask1")() _this refers to window. But to me that does not make sense. I have only been doing javascript for 1.5 years and these little nuances (coming from Java/C#) are difficult to understand.

  • 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-31T00:14:53+00:00Added an answer on May 31, 2026 at 12:14 am

    Your initial assertion:

    When calling a method directly, this refers to the containing object

    is not really true. The this pseudo-variable is bound based on the object reference used to get to the function (if any). If you instantiate a function as a property value of some object, and then call it by reference via that object property, then yes, this is bound to that object. However, if you copy the function as a value to a property of another object, then a call via that other object will have that object as this (it’s always a “Who’s on first?” issue when discussing this topic :-).

    In any case, you can use .call() or .apply() to force the this value to be whatever you want, regardless of how you get to the function. The bottom line is that in JavaScript there is never any inherent relationship between a function and any particular object. The binding of this is determined at every function call.

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

Sidebar

Related Questions

I have tough time making this design decision. I could go with traditional new
Is this correct? When calling method GetItems() on this item spec it is not
This one's a tough one - I have a JFrame that generates JTextFields. When
I am facing a really tough time doing this. I haven't done much on
I have 2 ViewControllers directly connected with a push segue. I am navigating from
C# 3.0 Extension methods add extensions to the base Type making calling that method
I have a form being displayed by a static method. This static method calls
This is a problem I've been having for a while and I'm hoping someone
This is a follow-up from this question : Effective Java 2nd Edition, Item 17:
I'm calling a worker method that calls to the database that then iterates and

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.