-1
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();

I have a method contain jquery each(), and I try to access this object's property, but i get undefined

I will need this property able to update

anyone know how to make this work?

Benjamin W
  • 2,658
  • 7
  • 26
  • 48
  • 1
    `this` inside `each` will refer to the current element in the set. Cache `this` to `that` and use `that.b`. – Tushar Sep 19 '16 at 15:34

1 Answers1

0

You need to bind this to function.

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);
Piotr Białek
  • 2,569
  • 1
  • 17
  • 26