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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:23:59+00:00 2026-05-20T00:23:59+00:00

I realise a question like this is asked pretty frequently (I’ve probably read every

  • 0

I realise a question like this is asked pretty frequently (I’ve probably read every one of them over the past few days trying to understand how to fix this) – but in this case, while I’m fairly confident I know why this is happening, I’m struggling to implement an actual solution.

I’m building a small application using Node.js but having trouble with creating an object with prototype functions that won’t lose their binding when they’re passed around.

Here’s what I have so far:

foo.js

var Foo = module.exports = function(server) {
    this.server = server;
    // some other stuff
};

Foo.prototype.send = function(data) {
    server.doStuff(data);
};

Foo.prototype.sendData = function(data) {
    // do stuff with data;
    this.send(data);
};

bar.js

var Bar = module.exports = function() {
    this.dataStore = // data store connection;
};

Bar.prototype.getSomething(data, callback) {
    this.dataStore.get(data, function(err, response) {
        callback(response);
    });
};

main.js

var Foo = require('./foo');
var Bar = require('./bar');
var aFoo = new Foo(server);
var aBar = new Bar();

// at some point do:
aBar.getSomething(data, aFoo.sendData);

As you can probably imagine, passing that aFoo.sendData function for use as a callback causes it to lose its binding to aFoo, so it is unable to find the ‘send’ function on Foo.

How would I modify Foo so that sendData maintains its binding to Foo? Is there a better way to structure this code so that this isn’t necessary?

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

    You simple wrap the call to aFoo.sendData in an anonymous function:

    aBar.getSomething(data, function () {
        aFoo.sendData();
    });
    

    Because your referencing aFoo.sendData, the reference to this inside sendData is no longer aFoo. When using the anonymous function, you’re not referencing the function; you’re simply invoking it as a method on the aFoo instance, so this is still aFoo.

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

Sidebar

Related Questions

I realise that this is a very basic question, but it is one which
Ok so I realize that this is a pretty vague question, but bear with
Sorry I know similar question have been asked many times before but like most
I realize that there was a similar question asked here , but this is
I realize that this question is impossible to answer absolutely, but I'm only after
I realize that the query this question is looking for won't be enough to
I realize this perhaps a naive question but still I cant figure out how
I realize there's no definitely right answer to this question, but when people talk
I'm writing this question from the standpoint of an ASP.NET application. However I realize
I realise this is not the ideal place to ask about this in terms

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.