When I call the method getResult it returns an undefined value. What am I doing wrong?
var MyObjectResult = {"Success":1, "Fail":2, "Timeout":3, "None":4}
function MyObject()
{
this.result = MyObjectResult.None;
this.timout = 15;
this.getResult = function ()
{
// Some calculation here and changing result
// Logging (this.result shows that result has value of 1)
this.result = MyObjectResult.Success;
return this.result;
}
}
var myObject = new MyObject();
var result = myObject.getResult();
// result is undefined
I see nothing wrong with the code as posted, so I’m going to take a guess about what is in the code that you don’t show:
Is the missing calculation code doing an ajax request (or some other asynchronous processing) and setting
this.resultin its success function? If so, thegetResult()function will return immediately, before your aysnc processing has run its success or failure function to updatethis.result. If the logging mentioned in your comment occurs in the success/failure function then it would have the correct value.