可能是重复问题:
在Javascript中,为什么“this”运算符不一致?
我有以下类:
function Chat(some, nick, url) {
this.socket = null;
this.Nickname = nick;
this.Url = url;
this.Connect = function () {
socket = io.connect(this.Url);
socket.on('connect', function (data) {
var p = this.Nickname; //this.Nickname is undefined why?
// how can I acess to the Nickname variable or function?
}
};
}
如何从连接回调函数中访问实例变量或函数?
this.Connect
上面,你应该能够做var obj = this;
然后在函数内使用该变量。 - Danny