在对象变量方面,我不了解JavaScript范围。我定义了以下类:
class DataHandler {
constructor(boardIDA, boardIDB, accessToken, eventA, eventB) {
this.times = [];
this.eventA = eventA;
this.eventB = eventB;
this.apiURLA = "https://api.particle.io/v1/devices/" + boardIDA + "/events/" + eventA + "/?access_token=" +
accessToken;
this.apiURLB = "https://api.particle.io/v1/devices/" + boardIDB + "/events/" + eventB + "/?access_token=" + accessToken;
this.eventListenerA = new EventSource(this.apiURLA);
this.eventListenerB = new EventSource(this.apiURLB);
this.addListener(this.eventListenerA,
eventA);
this.addListener(this.eventListenerB, eventB);
}
addListener(eventSrc, eventName) {
eventSrc.addEventListener(eventName, function(info) {
alert(eventName);
var parsedData = JSON.parse(info.data);
this.times.push(parsedData);
});
}
}
问题在于this.times
。我在构造函数中声明了它。但是,我似乎无法在addListener
中访问它 - 我收到一条错误消息,说我试图在push
上致电undefined
。我做错了什么?
答案 0 :(得分:2)
您没有在CV = c("a","b","c","d")
AL = list(c(1,2),c(2,3,4),c(2))
CV[AL[[1]]]
[1] "a" "b"
sapply(CC,'[',AL)
中访问它,您试图在匿名事件处理程序中访问它,该处理程序使用不同的addListener
进行调用。
this