我创建了一个JavaScript类(使用Prototype),如果在指定的秒数内没有鼠标移动,它将把页面状态设置为空闲状态。当鼠标移动时,该类将通过向侦听器列表发送消息来“唤醒”页面。Javascript /原型范围混乱
我不明白的事情是,this.handlers
是有效的在一个函数(setIdle
),而不是另一个(setActive
)。下面的注释代码说明我的问题:
var IM2 = Class.create({
handlers: null,
initialize: function(callback, frequency) {
this.handlers = [];
Event.observe(document, "mousemove", this.sendActiveSignal);
Event.observe(document, "keypress", this.sendActiveSignal);
setInterval(this.sendIdleSignal.bind(this), 5000);
},
addListener: function(h) {
console.log(this.handlers.size()); // it's 0 here, as expected
this.handlers.push(h);
console.log(this.handlers.size()); // it's 1 here, as expected
},
sendIdleSignal: function(args) {
console.log("IDLE");
this.handlers.each(function(i){
i.setIdle();
})
},
sendActiveSignal: function() {
// this.handlers is undefined here. Why?
this.handlers.each(function(r) {
r.setActive();
})
}
});
你有在对象末尾添加一个额外的逗号,这可能会产生一些奇怪的错误,但我怀疑这是问题所在。如果在sendActive()内部执行console.log(this),会得到什么记录? – tj111 2009-06-09 14:49:34