Created
February 6, 2014 05:51
-
-
Save ww24/8838967 to your computer and use it in GitHub Desktop.
V8 で行番号を取得する。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// test1 | |
console.log(getLineNumber()); | |
// test2 | |
var test = (function test() { | |
console.log(getLineNumber()); | |
return test; | |
})(); | |
var obj = { | |
hoge: test, | |
fuga: function () { | |
console.log(getLineNumber()); | |
} | |
}; | |
// test3 | |
obj.hoge(); | |
// test4 | |
obj.fuga(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function customPrepareStackTrace(error, structuredStackTrace) { | |
var trace = structuredStackTrace[0]; | |
return { | |
// method name | |
name: trace.getMethodName() || trace.getFunctionName() || "<anonymous>", | |
// file name | |
file: trace.getFileName(), | |
// line number | |
line: trace.getLineNumber(), | |
// column number | |
column: trace.getColumnNumber() | |
}; | |
} | |
function getLineNumber() { | |
var original = Error.prepareStackTrace, | |
error = {}; | |
Error.captureStackTrace(error, getLineNumber); | |
Error.prepareStackTrace = customPrepareStackTrace; | |
var stack = error.stack; | |
Error.prepareStackTrace = original; | |
return stack; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example の結果
参考