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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:27:03+00:00 2026-06-09T20:27:03+00:00

So, I created an object which makes an AJAX call to populate its properties

  • 0

So, I created an object which makes an AJAX call to populate its properties during the initialization phase. However, I am running into a very weird behaviour: I can print and see the property values fine within the $.ajax() scope, but any public method that returns the value of properties have a return value of “undefined”.

Here’s what the JS code looks like:

function MyFunction() {
   this.myProperty;
   this.init();
}

Myfunction.prototype.getPropertyValue = function () {
    alert(this.myProperty); // returns 'undefined'
}

Myfunction.prototype.init = function () { 
   $.ajax({
      type: 'get',
      url: "getProperty.php",
      dataType: "json",
      success: function(response) {
         this.myProperty = response[0].Property;
         alert(this.myProperty) // returns 'Property Name'
      }
   });
}

My thinking is that within the $.ajax() scope, ‘this’ is actually referring to something else. So, my question is how do I make sure that ‘this.myProperty’ is set and doesn’t lose its value once we get outside of the AJAX scope?

Any help is much appreciated.

  • 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-09T20:27:04+00:00Added an answer on June 9, 2026 at 8:27 pm

    Part of the reason why you’re getting “undefined” because of the way you establish the value:

    var MyFunction = function () {
       this.myProperty;
       alert(this.myProperty); // undefined!
       this.init();
    };
    

    When you declare properties (or variables) without specifying a value, they default to “undefined”. Instead:

    var MyFunction = function () {
       this.myProperty = false;
       alert(this.myProperty); // false
       this.init();
    };
    

    On to the ajax call. You are right that the scope of the callback is not the same as the object. this, in the ajax success function, refers to the jQuery-wrapped XHR object. When you call this.myProperty = response[0].Property, you are actually creating a new property on the ajax object and setting its value. To correct this, you can either use the context option of the jQuery ajax object, OR bind the callback function using the javascript bind method:

      success: function(response) {
         this.myProperty = response[0].Property;
      }.bind(this)
    

    … or:

       $.ajax({
          type: 'get',
          url: "getProperty.php",
          dataType: "json",
          context: this,
          success: function(response) {
             this.myProperty = response[0].Property;
          }
       });
    

    Try it here: http://jsfiddle.net/SnLmu/

    Documentation

    • bind() on MDN – https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/bind
    • jQuery.ajax() – http://api.jquery.com/jQuery.ajax/
    • Functions and Function Scope on MDN – https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question. I first created an object which extends NSObject, I provided
Hi i created an object in which onefield is lookup to user and other
I have a WPF Path object which is created by reading nodes from a
HI all, I created a .net object (ex: A ) which contain data have
hi i am updating the keyIndex array, which is created in object, while i
I'm using Castle DynamicProxy with Autofac. I have an object for which I've created
I've created a class library which exposes my back-end object model. I don't wish
I have created some python code which creates an object in a loop, and
I have created an empty json object having an array itemlist(which further contains itemid
I have created small test web application which makes use of LINQ to SQL.

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.