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 realize this question (or questions that look like this) have been asked over
I realize this question has probably been asked numerous times, but I have not
There have been a few variations of this question asked in the past few
One of my friends asked me this question,and I do else not know the
First of all, I realise fully that this question has been asked numerous times,
This question seems to have been asked before, but I feel like my situation
I realise this question has been asked before however the previous answers have gotten
This is a bit of a vague question I realise, but I've been looking
This question may seem blindingly obvious and I realise I am putting myself up
I realize this question is pretty basic, but I'm really stuck. I have a

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.