可能重复:
In Javascript, why is the “this” operator inconsistent?JavaScript类变量的作用域
我有下面的类:
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
感谢您的所有awnsers!有一切正确!但我只能选择一个。 – elranu