0

I have this OOP using javascript but the resulted data is undefined. Maybe someone can help me to get through this.

var OOP = OOP || {
      vars: {},
      elems: {},
      fn: {
        loadData: function() {
         $.post('URLOFPHP', 
          {} //This is blank because the URL Automatically loads all the resulted queries
          ).done(function(response){
            var newData = $.parseJSON(response); 
            return newData;
            //console.log(newData);
          });
        }
      }
};

$(function() {
  console.log(OOP.fn.loadData()); // This gives me undefined
});

But when I try below it works:

$(function() {
 $.post('URLOFPHP', 
  {} //This is blank because the URL Automatically loads all the resulted queries
 ).done(function(response){
    var newData = $.parseJSON(response); 
    //return newData;
    console.log(newData);
 });
});

Console.log of new Data is:

[Object, Object, Object]
  0: Object
     Name: "Test1"
     >_proto_: Object
  1: Object
     Name: "Test2"
     >_proto_: Object
  2: Object
     Name: "Test3"
     >_proto_: Object
   length: 3
aldrin27
  • 3,407
  • 3
  • 29
  • 43
  • `$.post` is asynchronous. But besides that, here is a simplified examples of your code: `function foo() { function bar() { return 42; } }` What do you think `foo` returns when it's called? – Felix Kling Nov 23 '15 at 02:56
  • It will return exactly as that code. – aldrin27 Nov 23 '15 at 03:02

0 Answers0