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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:37:39+00:00 2026-05-24T06:37:39+00:00

the object F has a function stored as this.fn and this.state.fn . can be

  • 0

the object F has a function stored as this.fn and this.state.fn. can be called successfully as f.fn() but not as f.state.fn()

function F( i, f ) {
        this.i = i;     
        this.state = { 'fn':f };
        this.f = f;
};                      
F.prototype.inc = function() { this.i++ };
F.prototype.fn = function() { this.state.fn() };
f1 = new F( 1, function() { console.log( this.i ); } );
f1.f();                 // this works
f1.inc();               // this works
f1.state.fn;            // prints the function
f1.fn();                // undefined!
f1.state.fn();          // undefined!

the problem seems to be that the function is stored in the object state, because this works:

f1.state.fn.call( f1 );
F.prototype.fn = function() { this.state.fn.call(this); };

which seems to imply that the this context within F.state.fn is not F but rather F.state – which to me is completely counter-intuitive – is this right!?

  • 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-24T06:37:39+00:00Added an answer on May 24, 2026 at 6:37 am

    Within a function, this depends entirely on how you called the function.

    When you call a function using dot notation from an object this will be automatically set to that object.

    If you say someObject.someChildObject.someFunction() then within someFunction() you’ll find this will be set to someChildObject.

    So in your example f1.fn() should result in this being f1 within fn(), but then within that function you say this.state.fn() – which will call state‘s fn() with this set to state.

    You can override this behaviour using call or apply.

    Another example just for your interest:

    function F( i, f ) {
            this.i = i;     
            this.state = { 'fn':f };
            this.f = f;
    };                      
    f1 = new F( 1, function() { console.log( this.i ); } );
    f1.f();   // works - 'this' will be f1
    var x = f1.f; // create a reference to the same function
    x();      // won't work - 'this' will probably be 'window'
    

    If you create a reference to a function originally defined as an object property and call the function via that reference then this will be whatever applies to your new reference. In my example the x reference is a global, which in practice means it belongs to the window object. What you can learn from this is that the function that f1.f() calls doesn’t really belong to f1 at all.

    Continuing that example:

    f2 = {};
    f2.i = "hello";
    f2.f = f1.f;
    f2.f(); // 'this' will be f2, so should log "hello"
    

    When you call f2.f(), you’ll find this is set to f2, and because I’ve set a property f2.i the function will log that property.

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

Sidebar

Related Questions

I'm using a COM object that has a function called GetImage. http://www.pdf-tools.com/asp/products.asp?name=P2IA When I
PHP has a var_dump() function which outputs the internal contents of an object, showing
When an object has various formats (XML,CSV) it can be represented in, where should
I get this error object has no attribute 'im_func' with this class Test(object): def
How do I see if a certain object has been loaded, and if not,
I am aware of this question already existing, but it has given me no
Is it possible in a VBA function (UDF) to create an object that has
I have class A and list of A objects. A has a function f
If an object has a property that is a collection, should the object create
My ContentValues object has string keys, and I would like to get a String[]

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.