I have some Firebug console function calls that I wanted to disable when Firebug wasn’t enabled, e.g. console isn’t defined. This works fine in IE6 and FF3, but not in Chrome:
var log;
if(console){
log = console.log;
}else{
log = function(){ return; }
}
I get an “Uncaught TypeError: Illegal Invocation” in Chrome =/
I read about the issue here, where you have to apply a context, which is kind of new to me… and I can’t seem to figure how to accomplish the above in all browsers…
Yes, you should persist the context :
What is happening is that the context (the
thisvalue), is implicitly set when you call a function, for example:In this case, you are calling a function that is defined as a property of an object, when the function is invoked, the
thisvalue is set to that object.Now as in your example, if you copy a reference of that method to a variable:
As you can see, the
thisvalue refers to the global object.So, to avoid this implicit behavior you can set the context explicitly, with the
callorapplyfunctions.