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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:13:04+00:00 2026-06-12T12:13:04+00:00

I cannot access the constructor of a class when I return from an asynchronous

  • 0

I cannot access the constructor of a class when I return from an asynchronous call using a callback.

In this case I cannot access the test variable defined in the Myclass1 constructor

I can’t find a solution for this, what am I doing wrong ?

var MyClass1 = function () {

    this.test = 5;
};
MyClass1.prototype = function () {
    //This is the function which calls another Myclass2 which contains the function for ajax //request
    test = function () {
        myClass2 = new MyClass2();
        myClass2.getRequest.call(this, callback);
    },
    //This is the call Back
    callback = function () {
        alert(this.test); /*<---Cannot access this,shows undefined.In firebug it show this.test is not defined*/
    };

    return {
        test: test,
        callback: callback
    }
}

MyClass2.prototype = function () {
    getRequest = function () {
        //make an async call set the success to the callback .I have propagated the data to //myclass1
        $.ajax({
            url: "test.html",
            success: callBack.call(this, data) //<--I call the callback using myClass1
        })
    }

    return {
        getRequest: getRequest;
    }
}
  • 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-12T12:13:05+00:00Added an answer on June 12, 2026 at 12:13 pm

    Ok, so there are many points:

    First

    On the prototype creation, please, declare variables. Right now you are creating “test”, “callback”, and “getRequest” global variables because you dont use “var”

    MyClass1.prototype = function () {
        var test = function () {
            // some code...
        };
    
        var callback = function () {
            // some code...
        };
    
        // And more code...
    };
    

    And the comma at the end of “test” declaration works because of it’s a operator, but I’m pretty sure it does not what you mean to do.

    Or you can directly create functions:

    MyClass1.prototype = function () {
        function test() {
            // some code...
        }
    
        function callback() {
            // some code...
        }
    
        // And more code...
    };
    

    Second

    You are assigning “prototype” property to a function

    MyClass1.prototype = function() { ... };
    

    This means the prototype of your class is a function, who has methods “call”, “apply”, “bind” but not “test” nor “callback”. Probably you wanted to create a Immediately-Invoked Function Expression (IIFE)

    MyClass1.prototype = (function() {
        function methodA() {
            // some code...
        }
    
        return {
            methodA: methodA
        };
    })();
    

    Or a simple object:

    MyClass1.prototype = {
        methodA: function() {
            // some code...
        },
        methodB: function() {
            // some code...
        }
    };
    

    Third

    I don’t understand what is your code doing, where is “MyClass2” defined, it extends “MyClass1”?

    Fourth

    You are assigning the “test” property on MyClass1’s prototype to a function, but then, on the constructor you assign the “test” property to a number, maybe you wanted to use different properties.

    Fifth

    On this line:

    success: callBack.call(this, data)
    

    You are invoking function “callback” (I don’t know where it does come from), I repeat, you are invoking it, not setting it as a callback, you’re just calling the function and telling $.ajax than the callback will be the value returned by “callback.call(this, data)”, probably “undefined”.

    If you wanted to set “callback” function as callback for the ajax petition you need to pass a function and inside of this function you do whatever you want to do when the array arrives, in this case invoke “callback”, but you must save “this” variable in order to use it:

    var self = this;
    $.ajax({
        url: "test.html",
        success: function(data) {
            callBack.call(self, data);
        }
    })
    

    I suppose data variable comes from the ajax petition…

    As you see is really difficult to give a accurate response when the code is not tested so the next time please, provide a fiddle with your code.

    If all my assumptions are right: this is a code like the one you need: http://jsfiddle.net/pw3hj/

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

Sidebar

Related Questions

I cannot access to server through RestClient. I have this code written in cURL
It seems that i cannot access $(this) inside jquery ajax success function. please see
this.Invoke((MethodInvoker)delegate { code; }); This results in exception as Cannot access a disposed object.
Possible Duplicate: Why is there no call to the constructor? I am using Visual
Possible Duplicate: Cannot access private member in singleton class destructor I'm implementing a singleton
If I have this kind of hierarchy: #include <iostream> using namespace std; template<class T>
I have a class Type which cannot be copied nor it contains default constructor.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
Anyone know a tool I can use to debug cannot access the file because
I'm setting up an onclick method and i cannot access an int that i

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.