Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript console in browsers [duplicate]

Possible Duplicate:
Create shortcut to console.log()

In javascript we can easy assign functions to variables, eg :


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

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

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


x = console.log

now when I use :


x("hello world")

I get :

TypeError : Illegal invocation

However the above example works perfectly fine in firefox.

Is this some sort of security related issue with chrome or something altogether different. On a sidenote, is there a way I can get a function which generates an output to browser console with the line number where the function was called ?

like image 892
lorefnon Avatar asked Sep 24 '26 13:09

lorefnon


1 Answers

Method assignments like in your example lose their this context.

Try...

var x = console.log.bind(console);
like image 137
alex Avatar answered Sep 27 '26 02:09

alex