1

This is my client JS script

var Chat = function(username) {

    that = this;            
    this.myUser = {"username":username};    
    this.socket = io(server);

    this.socket.on('connect', function(){   
        console.log(that.myUser);   // this is UGLY but this.myUser will give an error  
    });
}

any idea of how to get rid of that nasty "that = this" ?

yarek
  • 11,278
  • 30
  • 120
  • 219

1 Answers1

3

You could use bind():

var Chat = function(username) {

    this.myUser = {"username":username};    
    this.socket = io(server);

    this.socket.on('connect', function(){   
        console.log(this.myUser);
    }.bind( this ) );
};

But to be honest, I personally do not think, that this is much better.

Sirko
  • 72,589
  • 19
  • 149
  • 183