看到一篇帖子,感觉写的很牛,摘抄在这里了:(代码如下)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
(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();