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

 

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

 

posted on 2015-11-28 13:18  明天有风吹  阅读(2616)  评论(0编辑  收藏  举报

导航

+V atob('d2h5X251bGw=')

请备注:from博客园