Monday 15 March 2010

javascript - how to avoid that=this on JS using socket.io? -



javascript - how to avoid that=this on JS using socket.io? -

this client js script

var chat = function(username) { = this; this.myuser = {"username":username}; this.socket = io(server); this.socket.on('connect', function(){ console.log(that.myuser); // ugly this.myuser give error }); }

any thought of how rid of nasty "that = this" ?

you utilize bind():

var chat = function(username) { this.myuser = {"username":username}; this.socket = io(server); this.socket.on('connect', function(){ console.log(this.myuser); }.bind( ) ); };

but honest, not think, much better.

javascript socket.io

No comments:

Post a Comment