-
[JS] JavaScript get call stack traceprograming/Language 2019. 7. 16. 17:03
안녕하세요, Einere입니다.
(ADblock을 꺼주시면 감사하겠습니다.)
오늘은 자바스크립트로 호출 스택을 얻는 방법을 알아보도록 하겠습니다.
호출 스택
코드
function getStackTrace () { let stack = new Error().stack || ''; stack = stack.split('\n').map(function (line) { return line.trim(); }); return stack.splice(stack[0] == 'Error' ? 2 : 1); } getStackTrace()[2]; // get stack trace info 2 levels-deep
위와 같은 함수를 이용해, 호출 스택을 얻을 수 있습니다.
주의사항
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.
MDN에 따르면, Error.prototype.stack은 비표준이라고 합니다.
참고
How to get result of console.trace() as string in javascript with chrome or firefox?
console.trace() outputs its result on console. I want to get the results as string and save them to a file. I don't define names for functions and I also can not get their names with callee.caller...
stackoverflow.com
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Stack
Error.prototype.stack
The non-standard stack property of Error objects offer a trace of which functions were called, in what order, from which line and file, and with what arguments. The stack string proceeds from the most recent calls to earlier ones, leading back to the origi
developer.mozilla.org
'programing > Language' 카테고리의 다른 글
[JS] Object.prototype.function의 호출객체를 얻는 방법 (0) 2019.07.17 [JS] 함수선언식과 함수표현식 (0) 2019.07.16 [Functional] map, filter, reduce (0) 2019.07.15 [Functional] 제네레이터와 이터러블 (0) 2019.07.15 [Functional] 순회와 이터러블 (0) 2019.07.15 댓글