__LINE__相当于Javascript [英] __LINE__ equivalent in Javascript

查看:79
本文介绍了__LINE__相当于Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在Javascript中获取源代码行,如 __ LINE __ 代表C或PHP?

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

推荐答案

有一种方法,虽然更昂贵:抛出异常,立即捕获它,并从堆栈跟踪中挖出第一个条目。请参阅此处示例,了解如何解析跟踪。同样的技巧也可用于普通Java(如果代码是在打开调试信息的情况下编译的)。

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).

编辑:显然不是全部浏览器支持这个。好消息是(感谢评论,Christoph!)一些浏览器导出源文件名和行号直接通过 fileName lineNumber 错误对象的属性

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.

这篇关于__LINE__相当于Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆