【问题标题】:Is there a way to print out the line number of a js file in the console log有没有办法在控制台日志中打印出js文件的行号
【发布时间】:2012-03-06 14:16:48
【问题描述】:

出于调试目的,我通常使用console.log('line number #') 之类的东西。

不确定这是否是处理它的最佳方式,但我认为如果我可以打印出我放置 console.log() 的行的行号动态会很有帮助。

假设:

1    //do something
2    if(file){
3        console.log('Line 3');
4        $('#uploads').css({ 'height' : 'auto' });
5    } else { 
6       console.log(getLineNumber());   //just an example
7       $('#tags').val(temp);
8    }

在上面,如果我碰巧删除了第 1 行,第 3 行在技术上将是不正确的,因为行号减 1,但日志仍将显示 3。但在第 6 行,假设 getLineNumber() 返回行号,那么即使删除了上面的一行,它仍然有意义。

那么有没有像getLineNumber() 这样的简单方法?

【问题讨论】:

    标签: console google-chrome-devtools


    【解决方案1】:

    您可以为此使用onerror 事件处理程序。

    查看本页的最后一个示例:http://www.tutorialspoint.com/javascript/javascript_error_handling.htm

    示例的直接链接:http://www.tutorialspoint.com/cgi-bin/practice.cgi?file=javascript_40

    【讨论】:

    • 但有些情况并不完全是错误。也许我只想检查脚本执行是否在if, else 条件或循环内完成。
    • 没有干净的解决方案,但像这样的解决方法new Error().lineNumber -> stackoverflow.com/questions/2343343/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 2019-01-16
    • 2016-09-16
    • 2019-01-21
    • 2020-01-22
    • 1970-01-01
    相关资源
    最近更新 更多