stratboy (5) [Avatar] Offline
#1
Hi! Ch 04, p. 142, notify function:

jsEvent.EventRouter.prototype.notify=function(e){
var lsnrs=this.lsnrs;
for(var i=0;i<lsnrs.length;i++){ >
var lsnr=lsnrs[i];
lsnr.call(this,e);
}
}

I can't figure out what's the value of this here. I've tryed several alerts but I didn't found it.

For exemple, if I nest a for in loop like this:

jsEvent.EventRouter.prototype.notify=function(e){
var lsnrs=this.lsnrs;
for(var i=0;i<lsnrs.length;i++){ >
var lsnr=lsnrs[i];

see(this);

lsnr.call(this,e);
}
}

function see(obj){
for(var prop in obj){ alert('name:'+prop+' value:'+obj[prop]); }
}

It alerts

name: lsnrs
value: the 2 array members (the 2 callback functions)

and nothing more. So, is it EventRouter? I don't think because if I add an arbitrary property to the constructor the see() loop doesn't alert it....

All the other pieces of the script are ok for me, but I also wanna know what's this here, or better, what the object notify() is passing to call() via the this keyword.

Any idea?
Pascarello (208) [Avatar] Offline
#2
Re: CH 04 question
Sorry been pulling way to many hours the past few weeks trying to get a project out the door. 8AM - 4:30AM has my day yesterday. I bet that code is great after midnight!

The this refers to jsEvent.EventRouter. If we were to just call the listener without the call we would be loosing our object scope. I think call is explained pretty well here: http://www.webreference.com/js/column26/call.html

Eric