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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:58:29+00:00 2026-06-13T18:58:29+00:00

Something has always bothered me about the way I do object-oriented coding in Javascript.

  • 0

Something has always bothered me about the way I do object-oriented coding in Javascript. When there’s a callback, I frequently want to reference the object which originally called the function, which leads me to do something like this:

MyClass.prototype.doSomething = function(obj, callback) {
    var me = this; // ugh
    obj.loadSomething(function(err, result) {
        me.data = result; // ugh
        callback(null, me);
    });
}

First off, creating the additional variable alway seemed… excessive to me. Furthermore, I have to wonder if it might end up causing problems (circular references? un-GCd objects?) by passing the “me” variable back to the callback.

Is there a better way to go about this? Is this approach evil?

  • 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-13T18:58:30+00:00Added an answer on June 13, 2026 at 6:58 pm

    This is what Function.bind() is for:

    MyClass.prototype.doSomething = function(obj, callback) {
        obj.loadSomething((function(err, result) {
            this.data = result;
            callback(null, this);
        }).bind(this));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is something that has always bothered me about PHP and I have never
One of the things that has always bothered me about <jsp:include..> is that it's
I've been using JSF for a while but there's something that has always confused
Something that has always bugged me is how unpredictable the setTimeout() method in Javascript
This is something which has always puzzled me. If I want to make a
Something that has always bothered me is doing more than one loop to manipulate
This is something that has always bothered me. Wouldnt it make more sense to
Has something changed in Perl or has it always been this way, that examples
I am sort of new to JavaScript and something has been bugging me. Say
I am sure there is a good way of solving this, but it's something

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.