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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:23:23+00:00 2026-05-26T12:23:23+00:00

I find myself assigning this to a variable so I can easily use it

  • 0

I find myself assigning “this” to a variable so I can easily use it in callbacks and closures.

Is this bad practice? Is there a better way of referring back to the original function?

Here is a typical example.

User.prototype.edit = function(req, res) {

  var self = this,
      db = this.app.db;

  db.User.findById('ABCD', function(err, user)) {

    // I cannot use this.foo(user)
    self.foo(user);
  });
};

User.prototype.foo = function(user) {

};

Do you normally use this approach or have you found a cleaner solution?

  • 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-26T12:23:24+00:00Added an answer on May 26, 2026 at 12:23 pm

    There are three main ways to deal with this in callbacks:

    1. Create a lexically-scoped variable, as you are currently doing

    The two most common names for this new variable are that and self. I personally prefer using that because browsers have a global window property called self and my linter complains if I shadow it.

    function edit(req, res) {
        var that = this,
        db.User.findById('ABCD', function(err, user){
            that.foo(user);
        });
    };
    

    One advantage of this approach is that once the code is converted to using that you can add as many inner callbacks as you want and they will all seamlessly work due to lexical scoping. Another advantage is that its very simple and will work even on ancient browsers.

    2. Use the .bind() method.

    Javascript functions have a .bind() method that lets you create a version of them that has a fixed this.

    function edit(req, res) {
        db.User.findById('ABCD', (function(err, user){
            this.foo(user);
        }).bind(this));
    };
    

    When it comes to handling this, the bind method is specially useful for one-of callbacks where having to add a wrapper function would be more verbose:

    setTimeout(this.someMethod.bind(this), 500);
    
    var that = this;
    setTimeout(function(){ that.doSomething() }, 500);
    

    The main disadvantage of bind is that if you have nested callbacks then you also need to call bind on them. Additionally, IE <= 8 and some other old browsers, don’t natively implement the bind method so you might need to use some sort of shimming library if you still have to support them.

    3. If you need more fine-grained control of function scope or arguments, fall back to .call() and .apply()

    The more primitive ways to control function parameters in Javascript, including the this, are the .call() and .apply() methods. They let you call a function with whatever object as their this and whatever values as its parameters. apply is specially useful for implementing variadic functions, since it receives the argument list as an array.

    For example, here is a version of bind that receives the method to bind as a string. This lets us write down the this only once instead of twice.

    function myBind(obj, funcname){
         return function(/**/){
             return obj[funcname].apply(obj, arguments);
         };
    }
    
    setTimeout(myBind(this, 'someMethod'), 500);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I find myself having this sort of pattern over and over: variable = try:
I find myself writing code that looks like this a lot: set<int> affected_items; while
I find myself doing this repeatedy. $jq(button).filter(function(){ return this.id.match(/^user_(\d+)_edit$/); }).click(function(){ var matches = this.id.match(/^user_(\d+)_edit$/);
I find myself lacking knowledge around the below. Take this class: public class MyClass
I find myself adding debugging print statements quite often -- stuff like this: print(a_variable_name:
I find myself writing code like this when I want to repeat some execution
I find myself constantly learning new things in web development and there is always
I find myself breaking this pattern all the time. YAGNI - You Ain't Gonna
I find myself frequently writing code like this in the HTML HEAD and other
I find myself often needing to use Integer.TryParse to test if a value is

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.