http://stackoverflow.com/questions/11386492/accessing-line-number-in-v8-javascript-chrome-node-js
1 Object.defineProperty(global, ‘__stack‘, { 2 get: function(){ 3 var orig = Error.prepareStackTrace; 4 Error.prepareStackTrace = function(_, stack){ return stack; }; 5 var err = new Error; 6 Error.captureStackTrace(err, arguments.callee); 7 var stack = err.stack; 8 Error.prepareStackTrace = orig; 9 return stack; 10 } 11 }); 12 13 Object.defineProperty(global, ‘__line‘, { 14 get: function(){ 15 return __stack[1].getLineNumber(); 16 } 17 }); 18 19 console.log(__line);
上面的代码应该输出 19
另外
__dirname 当前路径 /Users/dev08/test
__filename 当前文件名 /Users/dev08/test/sc.js
时间: 2024-12-15 22:11:15