Javascript OOP 变量作用域问题

标签 javascript oop scope

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

//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

最佳答案

如果您的查询是异步的,则对象会在这些变量更改之前构造。将您的警报移至正确的范围:

  //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

});

关于Javascript OOP 变量作用域问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16366208/

相关文章:

c# - 如何避免在变量赋值的情况下调用冗余?

javascript - 为什么我的内部函数可以访问外部全局范围变量?这不是违反范围/封闭吗?

javascript - jquery 中的 off() 临时关闭事件

javascript - 如果元素位于另一个对象数组中,则删除对象数组内包含数组的对象属性

.net - 如何知道一个变量对于另一个对象来说是否只是 "pointer"或者是否可以独立存在

java - 如何在子类实例方法中返回父类(super class)对象?

javascript - 如何在 v8 中创建真正的全局对象?

java - 为什么 Kotlin print 语句不需要范围澄清?

javascript - 在 Joomla (1.7.0) 中点击显示嵌套列表

javascript - 如何从javascript中的国家代码获取国家名称