我的代码中有一点问题.这里是 :
// We are in the constructor of my class
this.socket.emit('getmap', {name: name}, function(data){
this.mapData = data.map;
this.load();
});
Run Code Online (Sandbox Code Playgroud)
问题是该mapData属性未设置,实际上this是指名称空间Socket.我如何this.mapData通过此功能访问?
抱歉我的英语不好......
Ken*_*eth 10
您需要保存this对象的引用.回调内部this将引用调用该函数的对象.一个常见的模式是:
// We are in the constructor of my class
var self = this;
this.socket.emit('getmap', {name: name}, function(data){
self.mapData = data.map;
self.load();
});
Run Code Online (Sandbox Code Playgroud)