http://stackoverflow.com/questions/11386492/accessing-line-number-in-v8-javascript-chrome-node-jsjavascript
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);
上面的代碼應該輸出 19java
另外node
__dirname 當前路徑 /Users/dev08/testchrome
__filename 當前文件名 /Users/dev08/test/sc.jsspa