Here is code sample:
var eventStack = {};
function addEvent (fn) {
eventStack[fn] = fn;
}
function removeEvent (fn) {
delete eventStack[fn];
}
addEvent(alert);
addEvent(console.log);
addEvent(addEvent);
it works whatever function I define myself, but doesn’t work for console.log. Instead it’s replaced with _firebugignore.
I think there is some magic with toString property
EDIT hmng, I just run my code again, and It worked fine for console.log, previous time both key and value were replaced buy “_firebuignore”, I suppose its higgs bugson
Yes, there is some magic with
Function.prototype.toString: It’s implementation-dependent, it only needs to return a representation of the function which is FunctionDeclaration-syntax-like. And of course it can’t return a JS function for all those environment-builtin functions (likeconsole.log).However, I don’t think it is a good idea to identify a function by its string representation at all. It can easily happen that two different functions end up in the same string (examples: two identical function expressions, identical function declarations in different scopes – or closures, builtin-functions (
Array.prototype.toString.toString() == Function.prototype.toString.toString()).Instead, use an Array for your
eventStackand check for a function’s existance byindexOf().