posted: 03.26.03 7a•-,
For removeListeners you can also say this._listeners = ; instead of cycling through the whole array. Also, you might want to rename it to removeAllListeners as there is already a removeListeners proto on here that removes passed listeners (I do believe) ;). Actually, I think it has the addListeners too... but yeah, all handling of those listeners is done through that array so you can treat that array like any other array performing whatever operations on it you want molding it to be whatever array of listeners you want :) Is it wrong? Nah, its doing basically the same thing the addListener/removelister does, just using more at once. The only thing someone might consider wrong (which is how I did mine too) is from the use of the Object.prototype. It could be argued that these methods shouldnt be avaiable to all Object instances, but just to those which have been ABS initiated. Of course, in initiating those objects, you are copying those methods into the object as new seperate methods. Using the Object prototype lets you reference the same method for all all initiated ASB objects - this also lets you change any method used like this (as an obj proto) at runtime and have its effect reflected in the ASB init instances, whereas, if a copy, you'd still have the original copy contained within that object. blah blah blah these are fine :)
wow. thats what I call a long comment. thanks alot for that info seems like someone have been doing some really deep research on that un-documented future :)
I did some renaming on the protos, so it suits the purpose better. I saw the other protos and wanted to display another way of doing it ;)
So in other words addListener works as a push function does for arrays?
And the question is, do addListener(liten_obj) act faster the push(listen_obj).. Needs futher investigation :)
posted: 03.26.03 3p•-,
using direct array methods are faster, since, for addListener, all its doing is acting as a wrapper for push... actually, more than that. Not only does addListener push the passed variable on the array, but it also makes sure that the listener is not repeated by first going through the array and removing the listener if it already exists, then pushing it on. So addListener removes and pushes. Just pushing alone would 1) prevent the overhead of that extra function call of addListener, and 2) prevent the call of removeListener which would cycle through the entire array of listeners to complete. Of course in using just push, you are allowing yourself to possibly have duplicates in the _listener array which, when an event is broadcasted, would act on that duplicate once for each instance its in the array, not just once. Would you want that? maybe!
posted: 07.03.03 8p•-,
senocular, in regards to your statement that using Object.prototype to define ASBroadcaster methods could be perceived by some as an error, what about using your isASBroadcaster prototype inside custom ASB methods to check whether the calling object is actually an ASB and therefore has "permission" to use those methods?
posted: 07.04.03 9a•07.04.03 10a,
Yes, thats also an option. For this example though, because its using a property set by ASBroadcaster (_listeners) chances are it wont work on any other objects since they wouldnt have that property... well not unless you gave it to them which is an option. But isBroadcaster does pretty much just that, checks to see if listeners exists - something you could add yourself without not even calling ASBroadcaster allowing you to fool one of these methods to think its working on an ASBroadcaster initiated object.
Bascially it relies a lot on faith that you will only attempt to use there object.prototype methods on a broadcasting object and have no desire to use it otherwise :)