2013-05-03 106 views
0

我正在使用alert()进行测试,但我无法让我的对象在我的for循环内部更改之后提醒我的变量的正确值。我想使用构造函数将值存储在对象中,但只有在开始时设置的空值被存储。可变范围的Javascript OOP问题

//declare the variables I'm going to use inside of my database query and for loop. 

$(function(){ 
    var uID = JSON.parse(localStorage.getItem('uID')); 
    var hood = ' '; 
    var gender = ' '; 
    var birthday = ' '; 
    var zip = ' '; 
    Parse.initialize("ivHLAO7z9ml1bBglUN*******yCgKM2x","gNeGt04lU7xce*****BsIBSCVj"); 

$("#mainDiv").on('click', '.interested', function(){ 
    //on click, use "uID" variable to query the parse database to get birthday, gender, neighborhood, and zip 
    var query = new Parse.Query(Parse.User); 
    query.equalTo("uID",uID); 
    query.find({ 
     success: function(results) { 
      for(i = 0; i < results.length; i++){ 
       //this is where the variable values declared at the beginning are supposed to be changed to the results of the query 
       hood = results[i].get("neighborhood"); 
       gender = results[i].get("gender"); 
       birthday = results[i].get("birthday"); 
       zip = results[i].get("zipCode"); 
      } 
     }//closes success 
    })//closes find 

    //my object constructor 

    function interested(neighborhood,sex, bDay, zipCode) { 
     this.hood = neighborhood; 
     this.gender = sex; 
     this.birthday = bDay; 
     this.zip = zipCode; 
    } 
    var intrstd = new interested(hood, gender, birthday,zip); 

    alert(intrstd.hood); 
    alert(intrstd.gender); 
    alert(intrstd.birthday); 
    alert(intrstd.zip); 
});//closes on 
+0

尝试在'success'函数内添加一个断点并查看它何时被触发与何时触发警报。 – 2013-05-03 20:00:33

回答

3

如果查询是异步的,那么在这些变量变化之前构造对象。将您的警报转移到正确的范围内:

//declare the variables I'm going to use inside of my database query and for loop. 
$(function() { 
    var uID = JSON.parse(localStorage.getItem('uID')); 
    var hood = ' '; 
    var gender = ' '; 
    var birthday = ' '; 
    var zip = ' '; 
    Parse.initialize("ivHLAO7z9ml1bBglUN*******yCgKM2x", "gNeGt04lU7xce*****BsIBSCVj"); 

    $("#mainDiv").on('click', '.interested', function() { 

     //on click, use "uID" variable to query the parse database to get birthday, gender, neighborhood, and zip 

     var query = new Parse.Query(Parse.User); 
     query.equalTo("uID", uID); 

     //my object constructor 
     function interested(neighborhood, sex, bDay, zipCode) { 
      this.hood = neighborhood; 
      this.gender = sex; 
      this.birthday = bDay; 
      this.zip = zipCode; 
     } 

     query.find({ 
      success: function (results) { 

       for (i = 0; i < results.length; i++) { 

        //this is where the variable values declared at the beginning are supposed to be changed to the results of the query 

        hood = results[i].get("neighborhood"); 
        gender = results[i].get("gender"); 
        birthday = results[i].get("birthday"); 
        zip = results[i].get("zipCode"); 
       } 

       var intrstd = new interested(hood, gender, birthday, zip); 

       alert(intrstd.hood); 
       alert(intrstd.gender); 
       alert(intrstd.birthday); 
       alert(intrstd.zip); 

      } //closes success 
     }) //closes find 

    }); //closes on 

}); 
+0

这很有用,谢谢。 – Spilot 2013-05-03 20:01:42