有没有办法在控制台日志中打印出一个js文件的行号 [英] Is there a way to print out the line number of a js file in the console log

查看:815
本文介绍了有没有办法在控制台日志中打印出一个js文件的行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了调试,我通常使用 console.log('line number#')



不确定是否是最好的处理方式,但我认为如果我可以打印出行的行号,我把 console.log()

假设:

 

code> 1 // do something
2 if(file){
3 console.log('Line 3');
4 $('#uploads')。css({'height':'auto'});
5} else {
6 console.log(getLineNumber()); //只是一个例子
7 $('#tags')。val(temp);
8}

在上面的例子中, 3在技术上是不正确的,因为行号减1,但日志仍然显示3.但是在第6行中,假设 getLineNumber()返回行号,



因此,有一个简单的方法,如 getLineNumber()

解决方案

您可以使用 onerror



请参阅此页面上的最后一个示例: http://www.tutorialspoint.com/javascript/javascript_error_handling.htm



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

For debugging purposes, I usually use something like console.log('line number #').

Not sure if it's the best way to handle it but I think it would be helpful if I can just print out the line number of the line where I'm putting the console.log() dynamically.

Let's say:

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    }

In the above, if I happen to remove line 1 for instance, line 3 will be technically incorrect as the line number is decremented by 1 but the log will still show 3. But in line 6, suppose getLineNumber() returns the line number, then it will still make sense even after a line above has been removed.

So is there an easy way which acts like getLineNumber()?

解决方案

You can use onerror event handler for that.

See the last example on this page: http://www.tutorialspoint.com/javascript/javascript_error_handling.htm

Direct link to example: http://www.tutorialspoint.com/cgi-bin/practice.cgi?file=javascript_40

这篇关于有没有办法在控制台日志中打印出一个js文件的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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