2013-05-03 115 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