How can I determine the current line number in JavaScript?

前端 未结 8 1652
自闭症患者
自闭症患者 2020-11-27 13:46

Does JavaScript have a mechanism for determining the line number of the currently executing statement (and if so, what is it)?

相关标签:
8条回答
  • 2020-11-27 14:12

    var thisline = new Error().lineNumber

    If that doesn't work in whatever environment you're using, you can try:

    var stack = new Error().stack

    Then hunt through the stack for the line number.

    0 讨论(0)
  • 2020-11-27 14:14

    Purely one can't get the line number out of Error.stack, because in Angular the line number is the line number of the compiled code. But one can get the info in which method the error was created. The class Logger in this code snippet add this piece of information to a new logbook entry.

    https://stackblitz.com/edit/angular-logger?file=src/app/Logger/logger.ts

    0 讨论(0)
提交回复
热议问题