0

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
console.log(result);

1
  • 1
    I see nothing wrong with the code above. Could the problem be in the missing calculation code? If you run the above as is does it work for you? Commented Nov 30, 2011 at 4:51

4 Answers 4

2

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.result in its success function? If so, the getResult() function will return immediately, before your aysnc processing has run its success or failure function to update this.result. If the logging mentioned in your comment occurs in the success/failure function then it would have the correct value.

Sign up to request clarification or add additional context in comments.

Comments

1

Strange.Its working for me:

http://jsfiddle.net/y5Yk7/

1 Comment

yes Marius just now i tried its working for us..Hope he is facing browser compatibility issue :-)
-1

Leaving out the quotes around Success, Fail, Timeout, and None should get it working.

I've set up a JSFiddle example; it is working perfectly fine for me.

4 Comments

Are you sure that that code is causing the error? Did you try to run the Fiddle I mentioned? That works perfectly fine for me ...
Quoting the keys has nothing to do with it.
The quotes in the data declaration is perfectly legal and not causing any issues.
@jfriend00 And the same applies for left-out quotes which are also perfectly legal and do not cause any issues.
-1

perhaps 'this' could have a different meaning within your function? so:

var MyObjectResult = {"Success":1, "Fail":2, "Timeout":3, "None":4}

function MyObject()
{
   this.result = MyObjectResult.None;
   this.timout = 15;
   var mythis = this;
   this.getResult = function ()
   {
      mythis.result = MyObjectResult.Success;
      return mythis.result;
    }
}

var myObject = new MyObject();
var result = myObject.getResult();

2 Comments

(I didn't downvote, but) The code as shown in the question shouldn't have any problem with this - calling getResult() as a property of myObject will ensure this has the right value within the function.
Yeah, my mistake, I did a little research on 'this' afterwards as I am more familiar with jquery's $(this) (yes I know that they are related). I thought it might be similar to some situations I have come across myself, but it turns out that the way the object is being called/created should work fine. shrug

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.