Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Based on the code you've given, I'm making the following assumptions:</p> <h2>Assumption 1:</h2> <p>You've initialized test to a function that returns some object or function, but haven't included that code in your question. This is called a constructor. I assume it looks something like:</p> <pre><code> function test(){ //do some work here return this; } //or var test = function(){ //do some work here return this; } </code></pre> <p>I assume this because you're able to set properties of the prototype without throwing errors, which the above would allow you to do.</p> <h2>Assumption 2:</h2> <p>I assume you're trying to go for some object oriented solution that involves more than one instance of your test object. This assumption is based on the fact that you are aiming to use prototypal inheritance, which is less important if your 'test' function is meant to have only one version(this is called a singleton).</p> <p>Based on these assumptions, the problem is that you're not creating an instance of your test object. The following code should work fine(relies on assumption 1);</p> <pre><code> var testInstance = new test(); testInstance.build(); // wait for AJAX to return and execute success handler testInstance.property // returns a proper value </code></pre> <p>If build is something you'll do only once and something you want to do for every instance of the test object, then you can put it into the constructor itself:</p> <pre><code> function test(){ this.build(); return this; }; var testInstance = new test(); // wait for AJAX to return and execute success handler testInstance.property // returns a proper value </code></pre> <p>Here's a full test, using <code>setTimeout</code> as the asynchronous call</p> <pre><code> function Test(){ //do anything return this }; Test.prototype.getName = function(){ var self = this; //simulate AJAX with setTimeout setTimeout( function(){ self.testName = "Ahmad Jamal"; console.log('name set'); }, 1000); }; Test.prototype.build = function(){ this.getName(); console.log(this.testName); // this is undefined, as the setTimeout is still waiting }; var test = new Test(); test.build(); // now wait to see 'name set' in the console test.testName; // returns "Ahmad Jamal" </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload