Javascript console in browsers

Possible duplicate:
Create shortcut for console.log ()

In javascript, we can easily assign functions to variables, for example:


a = function(){ alert("hello world");}
b = a;

      

Now both a and b can be used interchangeably to generate a warning message.

But when creating a cross browser wrapper, I noticed a very peculiar behavior in chrome:


x = console.log

      

now when i use:


x("hello world")

      

I get:

TypeError: Illegal invocation

However, the above example works fine in firefox.

This is some kind of security issue, with chrome or something else entirely. In sidenote, is there a way to get a function that generates output to the browser console with the line number where the function was called?

+3


source to share


1 answer


Method assignments like your example lose context this

.

Try ...



var x = console.log.bind(console);

      

+2


source







All Articles