nodejs 像 C 语言那样输出当前代码的行数

 

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

相关文章
相关标签/搜索