Jquery无法访问对象内部的属性

jquery couldn't access property inside of object

本文关键字:内部 属性 对象 访问 Jquery      更新时间:2023-09-26
function foo(){
    var a = 1;
    this.b = 2;
    this.c = function(){
        alert(a);
        alert(this.b);
        $('.ei').each(function(){
            alert(a);
            alert(this.b);//undefined <-- i need this to be update to 3
        });
    }
}
var obj = new foo;
obj.b = 3; //update this property before call method
obj.c();

我有一个方法包含jquery each(),我试图访问这个对象的属性,但我得到未定义

我需要这个属性能够更新

有人知道如何使它工作吗?

您需要将此绑定到函数。

this.c = function(){
    alert(a);
    alert(this.b);
    $('.ei').each(function(){
        alert(a);
        alert(this.b);//undefined <-- i need this to be update to 3
    }.bind(this));
}.bind(this);