当缩小JavaScript文件时,是否可以以任何方式动态获取错误的行号?
目前,所有错误均记录在第0行。
答案 0 :(得分:4)
没有。这就是为什么你不应该在开发/调试过程中使用缩小代码。
答案 1 :(得分:1)
您可以美化缩小的代码。在chrome检查器中,它是{}
按钮,称为“漂亮打印”。
但是,美化此代码并不意味着它会尊重您的原始代码。
因此,我会说ThiefMaster所说的内容:在开发/调试过程中不要使用缩小代码。
答案 2 :(得分:1)
您希望调试缩小脚本的一个原因是,如果您使用闭包编译器来优化它,并且优化过程会导致错误。 对于在堆栈跟踪中提供列(Chrome,IE)的浏览器,您可以执行以下操作:
/*@const*/ //for closure-compiler
DEBUG=2 // 0=off, 1=msg:file:line:column, 2=msg:stack-trace
if(DEBUG){
/*@const @constructor*/
Object.defineProperty(window,'__stack__',{get:function(){
try{_ფ_()}catch(e){return e.stack.split(":")}
}})
/*@const @constructor*/
Object.defineProperty(window,'__file__',{get:function(){
var s=__stack__,l=s.length
return (isNaN(s[l-2]))?s[l-2]:s[l-3]
}})
/*@const @constructor*/
Object.defineProperty(window,'__line__',{get:function(){
var s=__stack__,l=s.length
return (isNaN(s[l-2]))?s[l-1]:s[l-2]
}})
/*@const @constructor*/
Object.defineProperty(window,'__col__',{get:function(){
var s=__stack__,l=s.length
return (isNaN(s[l-2]))?"NA":s[l-1]
}})
/*@const @constructor*/
Object.defineProperty(window,'LOG',{
get:function(){return out},
set:function(msg){if(DEBUG>1)out=msg+"\t-\t"+__stack__
else out=msg+" in file:"+__file__+" @ Line:"+__line__+", Column:"+__col__
console.log(out)}
})
}//end if(DEBUG)
用法:LOG="my message"
将“我的消息”连同行号和文件一起写入控制台或获取最后一个日志alert(LOG)
您可以使用v8(chrome,node.js)
进一步了解杂草/*@const @constructor*/ Object.defineProperty(this,'__stack',{get:function(){var o=Error['prepareStackTrace'],e=new Error,s;Error['prepareStackTrace']=function(_,s){return s},Error['captureStackTrace'](e,arguments['callee']),s=e['stack'],Error['prepareStackTrace']=o;return s}})
/*@const @constructor*/ Object.defineProperty(this,'__col__',{get:function(){return __stack[1]['getColumnNumber']()}})
/*@const @constructor*/ Object.defineProperty(this,'__eval_orig__',{get:function(){return __stack[1]['getEvalOrigin']()}})
/*@const @constructor*/ Object.defineProperty(this,'__file__',{get:function(){return __stack[1]['getFileName']()}})
/*@const @constructor*/ Object.defineProperty(this,'__func__',{get:function(){return __stack[1]['getFunctionName']()}})
/*@const @constructor*/ Object.defineProperty(this,'__function__',{get:function(){return __stack[1]['getFunction']()}})
/*@const @constructor*/ Object.defineProperty(this,'__is_constructor__',{get:function(){return __stack[1]['isConstructor']()}})
/*@const @constructor*/ Object.defineProperty(this,'__is_eval__',{get:function(){return __stack[1]['isEval']()}})
/*@const @constructor*/ Object.defineProperty(this,'__is_native__',{get:function(){return __stack[1]['isNative']()}})
/*@const @constructor*/ Object.defineProperty(this,'__is_top_level__',{get:function(){return __stack[1]['isTopLevel']()}})
/*@const @constructor*/ Object.defineProperty(this,'__line__',{get:function(){return __stack[1]['getLineNumber']()}})
/*@const @constructor*/ Object.defineProperty(this,'__method__',{get:function(){return __stack[1]['getMethodName']()}})
/*@const @constructor*/ Object.defineProperty(this,'__this__',{get:function(){return __stack[1]['getThis']()}})
/*@const @constructor*/ Object.defineProperty(this,'__type__',{get:function(){return __stack[1]['getTypeName']()}})