1
function makeHttpRequest(url, success) {
 $.ajax({
        url: url,
   dataType: "jsonp",
crossDomain: true,
   mimeType: 'application/javascript', 
      async: false,
    success: success
  });
} 

   var actions = {
       get_min_hit_list_bounty: function (user_id) {
          makeHttpRequest("get_min_hit_list_bounty?target_id=" + user_id + "&", function (data) {
             var data = data['body'],
                xml = convert(data);
             this.min_cost = $(xml).find('min_cost').text();

         this.cost = function () {
            return this.min_cost;
         }
      });
   }
};
var myBounty = new actions.get_min_hit_list_bounty(user);
alert(myBounty.cost());

I cannot return anything from this type of object. I've read plenty on using prototype and using "this" to make it public, but I'm not getting any where. Could someone please explain why this does not work?

TypeError: Object [object Object] has no method 'cost'

9
  • 2
    You define function in makeHttpRequest() callback function, not in actions.get_min_hit_list_bounty object Commented Aug 12, 2012 at 22:04
  • I tried this.cost after the }); but its comes up undefined. Commented Aug 12, 2012 at 22:10
  • update your question with that version of the code Commented Aug 12, 2012 at 22:19
  • There are many more functions in the actions than listed here. Commented Aug 12, 2012 at 22:25
  • well, I've explained why the code in your question doesn't work. What do you want to know now? Commented Aug 12, 2012 at 22:27

2 Answers 2

1

This has nothing to do with object constructors.

Most importantly you're confusing how asynchronous calls work (the first A in AJAX!). The anonymous function in makeHttpRequest is called with the result of the request - which could be any time after the rest of the code executes, or not at all.

You'll need to make your code event driven - ie you make the AJAX request and run code when the response is returned.

for example:

var onSuccess = function(data) {
    var data = data['body'],
    xml = convert(data);
    // ... etc
}

makeHttpRequest(url, onSuccess);
Sign up to request clarification or add additional context in comments.

3 Comments

Tried it this way but still no luck.
get_min_hit_list_bounty : function(user_id){makeHttpRequest("get_min_hit_list_bounty?target_id=" + user_id + "&", get_min_hit_list_bounty); var get_min_hit_list_bounty = function(data){ var data = data['body'],
Well it looks like you're referencing get_min_hit_list_bounty before it's defined.
0

I believe it should look like this:

var actions = {
       get_min_hit_list_bounty: function (user_id) {
            this.userId = user_id;
            this.min_cost = 999999;

            this.cost = function () {
                return this.min_cost;
            };

            this.getMinCost = function(callback) {
                var me = this;

                var queryString = [
                "get_min_hit_list_bounty?target_id=",
                this.userId,
                "&"].join("");

                makeHttpRequest(queryString, function (data) {
                    var data = data['body'],
                    xml = convert(data);
                    me.min_cost = $(xml).find('min_cost').text();
                    callback.apply(me);
                });
            }
        }
};

var myBounty = new actions.get_min_hit_list_bounty(user);

myBounty.getMinCost(function() {
    alert(this.cost());
});

You have to understand how asynchronous code works. It is not just a set of instructions that executes consequtively. Think of it in terms of events and callbacks. Google should help.

5 Comments

Changed the alert to alert(myBounty.min_cost); Now it works. Thanks :)
@StormyWeather this.cost() or myBounty.cost() don't work properly?
no neither one of those. Uncaught TypeError: Object [object Object] has no method 'cost'
@StormyWeather check your code. this.cost() works for me. Maybe you copied previous version of my code, where I forgot to add 'cost' function.
Yes you are correct. this.cost() works now. Thank you for your time and help as well. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.