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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:58:59+00:00 2026-06-18T08:58:59+00:00

I am using node.js. I have this handlers.js file: exports.Handlers = function(prefix) { this.prefix

  • 0

I am using node.js. I have this handlers.js file:

exports.Handlers = function(prefix) {
    this.prefix = prefix;
    this.db = new DBInstance();
};

exports.Handlers.prototype.getItemById = function(id) {
    var item = this.db.getItemById(id, function(error, item) {
        item.name = this.prefix + item.name;
        ...
        ...
    });
};

When I call:

var h = new Handlers();
h.getItemById(5);

I get an error since the context isn’t the Handlers and this.prefix doesn’t exists. I can fix it using this:

exports.Handlers.prototype.getItemById = function(id) {
    var scope = this;
    var item = this.db.getItemById(id, function(error, item) {
        item.name = scope.prefix + item.name;
        ...
        ...
    });
};

Is there any better way to pass the context to the callback?
What is the nodejs common way to pass the context to the callback?

  • 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-06-18T08:59:00+00:00Added an answer on June 18, 2026 at 8:59 am

    Node implements ECMAScript 5, which has Function.bind().

    I think that is what you are looking for.

    exports.Handlers.prototype.getItemById = function(id) {
        var item = this.db.getItemById(id, (function(error, item) {
            item.name = this.prefix + item.name;
            ...
            ...
        }).bind(this)); //bind context to function
    };
    

    This works, however when using a closure as a callback, like you are doing, the most common way this is done is by storing the context in a variable that can be used in the closure.

    This method is more common because many times callbacks can be deep and calling bind on each callback can be heavy; whereas defining self once is easy:

    SomeObject.prototype.method = function(id, cb) {
        var self = this;
        this.getSomething(id, function(something) {
            //self is still in scope
            self.getSomethingElse(something.id, function(selse) {
                //self is still in scope and I didn't need to bind twice
                self.gotThemAll(selse.id, cb);
            });
        });
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Expressjs w/ node.js and I have a route like this: users.init(app.db, function()
I have a strange problem. I do this query in a node.js server using
I have the following to execute shell commands using Node: function puts( error, stdout,
I have the following express node.js app. It's using the 'redis' npm package. app.get(/test,function(req,res){
So I am using node.js and socket.io. I have this little program that takes
I'm using node-postgres to make SQL queries with callback style. I have a function
I need to have this node in my SOAP Request (using 1.1): <CredentialsHeader xmlns=http://www.url.com/Services/P24ListingService11
I have a simple webapp using node.js that reads the database and displays this
I have a small project that I was using node-dirty for, but it's not
I'm using ExpressJs with Node.js and have put all my routes into a 'routes'

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.