Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__LINE__ equivalent in Javascript

Tags:

javascript

Is there any way to get the source line number in Javascript, like __LINE__ for C or PHP?

like image 242
too much php Avatar asked Jan 17 '09 09:01

too much php


People also ask

How do you repeat a line in JavaScript?

The . repeat() method returns a new string, which is the original string repeated the specified number of times.

What is at the end of a line of code in JavaScript?

The semi-colon, ; , in JavaScript represents an “end of statement” or “statement delimiter”. It tells JavaScript not to parse any more code, and to go ahead and execute the statement up to that point.

When to use this in js?

“This” keyword refers to an object that is executing the current piece of code. It references the object that is executing the current function. If the function being referenced is a regular function, “this” references the global object.

What is this in function?

The this keyword refers to the object the function belongs to, or the window object if the function belongs to no object. It's used in OOP code, to refer to the class/object the function belongs to For example: function foo() { this.


1 Answers

There is a way, although more expensive: throw an exception, catch it immediately, and dig out the first entry from its stack trace. See example here on how to parse the trace. The same trick can also be used in plain Java (if the code is compiled with debugging information turned on).

Edit: Apparently not all browsers support this. The good news is (thanks for the comment, Christoph!) that some browsers export source file name and line number directly through the fileName and lineNumber properties of the error object.

like image 118
David Hanak Avatar answered Oct 05 '22 07:10

David Hanak