Javascript 对象变量在innerscope中没有更改

Javascript 对象变量在innerscope中没有更改,javascript,jquery,Javascript,Jquery,我制作了一个名为Product的函数,我用它在JavaScript中创建对象。现在,我有一个名为setProduct的函数,它从我的数据库(websql)获取数据,并在当前对象中从数据库设置所有这些数据。这是我的代码: function Product(nr, name, description, brand, category, price, tags, color, seccolor, image, sizes, db) { this.nr = nr;

我制作了一个名为Product的函数,我用它在JavaScript中创建对象。现在,我有一个名为setProduct的函数,它从我的数据库(websql)获取数据,并在当前对象中从数据库设置所有这些数据。这是我的代码:

function Product(nr, name, description, brand, category, price, tags, color, seccolor, image, sizes, db) {
    this.nr             = nr;
    this.name           = name;
    this.description    = description;
    this.brand          = brand;
    this.category       = category;
    this.price          = price;
    this.tags           = tags;
    this.color          = color;
    this.seccolor       = color;
    this.image          = image;
    this.sizes          = sizes;
    this.db             = db;

        Product.prototype.setProduct = function(id, cb) {
        var self = this;
        var query = "SELECT * from products WHERE id='"+id+"';"
        var res;
        this.db.exec(query, function(results) {
            res = results.rows.item(0);
            self.nr             = res.prod_nr;
            self.description    = res.prod_description;
            self.brand          = res.prod_brand;
            self.category       = res.prod_category;
            self.price          = res.prod_price;
            self.tags           = res.prod_tags;
            self.color          = res.prod_color;
            self.seccolor       = res.prod_sec_color;
            self.image          = res.prod_image;
            self.sizes          = res.available_sizes;
            self.name           = res.prod_name;
            cb(true);
        });
    };
} 
this.db=我的数据库对象。setProduct从该对象调用exec函数。这就是代码:

Database.prototype.exec = function(query, cb) {
        this.db.transaction(function(tx) {
            tx.executeSql(query, [], function(tx, results) {



                if (typeof(cb) == 'function') {

                        cb(results);
                    }
                });
            });
   }
我曾尝试使用几个console.log来查看数据丢失的位置,但不知何故我找不到它

这就是问题所在:

$("div#home").off('click').on('click','li', function() { 
            var product = prod;
            var prod_id = $(this).attr("id");
            prod.setProduct(prod_id, function(a) {
                if (a) {
                    console.log(prod.name); // console.log gives undefined
                }
            });
        });
}
找到了我的解决办法! 我已经将当前对象添加到一个新变量中,以便在innerscope中使用,但我忘记了使用它

$("div#home").off('click').on('click','li', function() { 
            var product = prod; // FOR THE INNER SCOPE
            var prod_id = $(this).attr("id");
            prod.setProduct(prod_id, function(a) {
                if (a) {
                    console.log(prod.name); // prod.name has to be product.name, as defined above
                    console.log(product.name); // WORKS!

                }
            });
        });
}

使用调试器而不是
console.log()
。PS:在中定义原型函数是非常奇怪的constructor@zerkms你是什么意思?好的,谢谢你的提示。我将把原型函数放在构造函数之外:)例如-google chrome developer tools js debugger PS:下面是一篇关于
console.log
何时可能对您撒谎的文章:我看不出您的对象范围有任何问题。您正在使用新关键字初始化产品吗?e、 g:
test=新产品()-如果是,下一步是确认
this.db.exec
回调具有正确的数据。@MattStone是的,我确实初始化了一个新的产品对象。很抱歉没有在我的帖子中提到这一点。this.db.exec具有正确的数据,因为它适用于我的所有其他函数,我会将其记录在console.log中