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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:53:25+00:00 2026-05-27T02:53:25+00:00

I can’t access the attribute of an instantiated class. The attribute is set using

  • 0

I can’t access the attribute of an instantiated class. The attribute is set using an AJAX call.

I am trying to define the class “CurrentUser”, and then set the attribute “userId” using AJAX.

Here I define the class CurrentUser, and give it the attribute userID:

function CurrentUser() {
    // Do an ajax call to the server and get session data.
    $.get("../../build/ajaxes/account/get_user_object_data.php", function(data) {
         this.userId = data.userId;
         console.log(data.userId);   // This will correctly output "1".
    }, "JSON");
}

Here I instantiate a CurrentUser named billybob. Notice how I can’t output billybob’s attribute:

// Instantiate the user. 
   var billybob = new CurrentUser();
   console.log(billybob.userId);     // This will incorrectly ouput "undefined".

I’ve checked the common errors with AJAX:

  • The AJAX call returns the data correctly as a JSON object. I can read the correct object in Firebug / Network console. The AJAX call also has a status of “200” and “OK”.

  • I can log the AJAX call’s data correctly, as seen in the first part of my code where I log data.userId.

  • 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-27T02:53:26+00:00Added an answer on May 27, 2026 at 2:53 am

    Maybe this clears it out:

    In your original code:

    function CurrentUser() {
        // Do an ajax call to the server and get session data.
        $.get("../../build/ajaxes/account/get_user_object_data.php", function(data) {
         this.userId = data.userId;
         console.log(data.userId);   // This will correctly output "1".
        }, "JSON");
    }
    

    You are creating an anonymous function on the fly, that will be later called by jQuery’s internals with this
    set to an ajax object. So this will be the ajax object inside the anonymous function, not billybob. So when
    you do this.userId = ... this means the ajax object which doesn’t have a userid property.

    jQuery will have no idea where you got your callback function from, so it cannot set the this automagically
    for you.

    What you must do is to save the billybob (or any CurrentUser instance) reference and use it in the callback like so:

    function CurrentUser() {
    var self = this;
        $.get("../../build/ajaxes/account/get_user_object_data.php", function(data) {
         self.userId = data.userId; //self refers to what this refered to earlier. I.E. billybob.
         console.log(data.userId, self.userid);   // This will correctly output "1".
        }, "JSON");
    }
    

    Also note that:

     var billybob = new CurrentUser();
       console.log(billybob.userId);   
    

    By the time you call console.log (I.E. instantly after creating billybob), the ajax request hasn’t been completed yet so it is undefined.

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

Sidebar

Related Questions

Can we close all known/unknown connections to database with the code? I'm using Access
Can you set the internal [[Class]] property of an ECMAScript object?
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can PHP PDO extension bind nested objects automatically ? I mean using foreign key
Can I register a .Net COM class with the SingleUse -flag? The reason I
Can anyone tell me what i am doing wrong? I am trying to get
Can anyone direct me to a good tutorial on how to set up virtual
Can you have submenus with the top level set to checkable in WPF? I
Can anyone help me trying to find out why this doesn't work. The brushes
Can I convert a pdf to pcl file with ghostscript? I'm using Ghostscript 9.01

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.