JavaScript使用console.log输出当前的行号

来源:互联网 发布:怎么选对戒 知乎 编辑:程序博客网 时间:2024/05/18 09:12
(function () {    if (Error.captureStackTrace && Object.defineProperty) {        var global = window;        Object.defineProperty(global, '__STACK__', {            get: function () {                var old = Error.prepareStackTrace;                Error.prepareStackTrace = function (error, stack) {                    return stack;                };                var err = new Error();                Error.captureStackTrace(err, arguments.callee);                Error.prepareStackTrace = old;                return err.stack;            }        });        Object.defineProperty(global, '__LINE__', {            get: function () {                return __STACK__[1].getLineNumber();            }        });        Object.defineProperty(global, '__FILE__', {            get: function () {                return __STACK__[1].getFileName();            }        });    }})();var test = function () {    console.log(__LINE__,__FILE__);};test();
0 0
原创粉丝点击