Possible Duplicate:
If Javascript has first-class functions, why doesn’t this work?
In Chrome, the following produces Uncaught TypeError: Illegal invocation:
g = console.log;
g(1);
Why does this happen, and why can’t I treat console.log like a regular object?
It happens because you’ve lost the reference to
console. You’re just callinglogdirectly, with no context. You can call the function in the context ofconsoleto make it work:Or, to save you from doing that every time, you can bind the function back to the
consoleobject:References:
Function.prototype.callFunction.prototype.apply(not used here, but still of interest)Function.prototype.bind