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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:29:23+00:00 2026-05-23T05:29:23+00:00

This has been eluding me. Take the following code: function tSt( val ) {

  • 0

This has been eluding me. Take the following code:

function tSt( val ) {
    this.val = val;
}

tSt.func = function( x ) {
    return x * 2;
}

tSt.prototype.func = function() {
    return tSt.func( this.val );
}

var tmp = new tSt( 5 );

console.log( tmp.func());  // 10

var other = tmp.func;

console.log( other());  // undefined

The reason I want to be able to do this is for something like the following scenario:

(function( funcarr ) {
    for ( var i = 0; i < funcarr.length; i++ )
        console.log( funcarr[i]());
})([ tmp.func ]);

How can this be accomplished?

Update:

Here’s a potentially more global solution, but I’m not sure if there are any drawbacks to doing it this way:

function tSt( val ) {
    this.val = val;
    this.func = this.func.bind( this );
}

tSt.func = function( x ) {
    return x * 2;
}

tSt.prototype.func = function() {
    return tSt.func( this.val );
}
  • 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-23T05:29:24+00:00Added an answer on May 23, 2026 at 5:29 am

    Use Function#bind:

    var other = tmp.func.bind(tmp);
    

    This ensures that whenever other is called, the this pointer will be tmp.

    Since bind is not implemented in all browsers, you may need a polyfill like the one given in the above link. (In fact, I recommend you go all the way and get an ES5 shim.)


    If you’re willing to abandon prototypal inheritance, which is necessary if you want to pass methods around without remembering to bind them, then I instead suggest the module pattern:

    function TSt(val) {
       // save a reference to our current "this" so that, even inside of methods
       // called from other contexts, we still have it.
       var that = this;
    
       that.val = val;
       that.func = function () {
           return that.val * 2;
           // Since we're using that instead of this, still works even without bind.
       }
    }
    
    var tmp = new TSt(5);
    var other = tmp.func;
    console.log(other()); // 10
    

    Indeed, another major benefit of this pattern is you can get private variables:

    function Encapsulated(mine, public) {
        var that = this;
    
        var private = mine * 2;
    
        that.public = public;
        that.compute = function () {
            return that.public * private;
        };
    }
    

    Now consumers of an Encapsulated instance have no access to mine or private, but only to public.

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

Sidebar

Related Questions

This has been driving me crazy for a few days. Why doesn't the following
This has been driving me nuts. I keep getting the following exception System.InvalidOperationException: The
This has been driving me absolutely nuts. I have a substitute function like this:
This has been asked before but I can't find a definitive answer, in code.
This has been driving me nuts for hours now. Consider the following test script
This has been a problem that I haven't been able to figure out for
This has been an adventure. I started with the looping duplicate query located in
This has been troubling me for a few years, and was recently exacerbated by
This has been asked before (question no. 308581) , but that particular question and
This has been confusing me for some time. With the advent of UTF-8 as

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.