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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:46:50+00:00 2026-05-17T22:46:50+00:00

I’m having a really rough time wrapping my head around prototypes in JavaScript. Previously

  • 0

I’m having a really rough time wrapping my head around prototypes in JavaScript.

Previously I had trouble calling something like this:

o = new MyClass();
setTimeout(o.method, 500);

and I was told I could fix it by using:

setTimeout(function() { o.method(); }, 500);

And this works. I’m now having a different problem, and I thought I could solve it the same way, by just dropping in an anonymous function. My new problem is this:

MyClass.prototype.open = function() {
    $.ajax({
        /*...*/
        success: this.some_callback,
    });
}

MyClass.prototype.some_callback(data) {
    console.log("received data! " + data);
    this.open();
}

I’m finding that within the body of MyClass.prototype.some_callback the this keyword doesn’t refer to the instance of MyClass which the method was called on, but rather what appears to be the jQuery ajax request (it’s an object that contains an xhr object and all the parameters of my ajax call, among other things).

I have tried doing this:

$.ajax({
    /* ... */
    success: function() { this.some_callback(); },
});

but I get the error:
Uncaught TypeError: Object #<an Object> has no method 'handle_response'

I’m not sure how to properly do this. I’m new to JavaScript and the concept of prototypes-that-sometimes-sort-of-behave-like-classes-but-usually-don’t is really confusing me.

So what is the right way to do this? Am I trying to force JavaScript into a paradigm which it doesn’t belong?

  • 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-17T22:46:51+00:00Added an answer on May 17, 2026 at 10:46 pm

    Am I trying to force JavaScript into a paradigm which it doesn’t belong?

    When you’re talking about Classes yes.

    So what is the right way to do this?

    First off, you should learn how what kind of values the this keyword can contain.

    1. Simple function call

      myFunc(); – this will refer to the global object (aka window) [1]

    2. Function call as a property of an object (aka method)

      obj.method(); – this will refer to obj

    3. Function call along wit the new operator

      new MyFunc(); – this will refer to the new instance being created

    Now let’s see how it applies to your case:

    MyClass.prototype.open = function() {
        $.ajax({ // <-- an object literal starts here
            //...
            success: this.some_callback,  // <- this will refer to that object
        });      // <- object ends here
    }
    

    If you want to call some_callback method of the current instance you should save the reference to that instance (to a simple variable).

    MyClass.prototype.open = function() {
        var self = this; // <- save reference to the current instance of MyClass
        $.ajax({ 
            //...
            success: function () {
                self.some_callback();  // <- use the saved reference
            }                          //    to access instance.some_callback
        });                             
    }
    

    [1] please note that in the new version (ES 5 Str.) Case 1 will cause this to be the value undefined

    [2] There is yet another case where you use call or apply to invoke a function with a given this

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

Sidebar

Related Questions

No related questions found

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.