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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:07:02+00:00 2026-06-12T18:07:02+00:00

I have the following code for the definition of a class var class1 =

  • 0

I have the following code for the definition of a class

var class1 = function () {
    this.classData = 'value1';
    this.func1 = function(callback) {
        $.ajax({
            'url': '/somewhere',
            'dataType': 'json',
            'type': 'POST',
            'data': {
                options: 'some text'
            },
            'success': function (data, textStatus, jqXHR) {
                callback(data); // <<<<<< THIS LINE
            }
        });
    };
};

and then I call the class like this

var obj1 = new class1();
obj1.func1(function (d) {
    this.classData = d;
});

But this doesn’t seem to work because, inside the sucess function, when the callback function is called at the line marked in the code above, it has it’s this object pointing to the window and not the obj1 value.

What am I doing wrong here, how do I fix it?

  • 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-12T18:07:03+00:00Added an answer on June 12, 2026 at 6:07 pm

    This isn’t really a scope problem but a context one. this, when your function is called, is the receiver of the function at the time of calling, not the object obj1.

    Do this :

    var obj1 = new class1();
    obj1.func1(function (d) {
        obj1.classData = d;
    });
    

    This is the right way.

    If your callbacks are all meant to have the instance of class1 as receiver, you may also do this :

    var class1 = function () {
        this.classData = 'value1';
        var _this = this;
        this.func1 = function(callback) {
            $.ajax({
                'url': '/somewhere',
                'dataType': 'json',
                'type': 'POST',
                'data': {
                    options: 'some text'
                },
                'success': function (data, textStatus, jqXHR) {
                    _this.callback(data); // note that I call the callback on the instance of obj1
                }
            });
        };
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function definition of the following form (within a large code): class
I have the following class definition: public static string SplitString(string someText) { var queryArray
I have following code for loading image from url in xml parsing endElement method
I have following code: def whatever(url, data=None): req = urllib2.Request(url) res = urllib2.urlopen(req, data)
I have the following code var section = new CustomConfigurationSection(); section.SectionInformation.Type = System.Configuration.NameValueFileSectionHandler; section.SectionInformation.SetRawXml(sectionXml);
Suppose we have following code defined in tester.py class Tester( object ): def method(
I have this situation where I declare inside my main class a function that
Hello I have following c++ code class classBase { public: int get1(){return 1;} int
I have the following code in a project I am doing for class. I
I have the following class definition: class IRenderable { public: virtual void Render( wxBitmap

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.