Heim  >  Artikel  >  Web-Frontend  >  Wie erhalte ich Stack-Trace-Informationen in JavaScript?

Wie erhalte ich Stack-Trace-Informationen in JavaScript?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-17 22:26:02383Durchsuche

How to Obtain Stack Trace Information in JavaScript?

stacktrace() Function for JavaScript Exceptions

Determining a JavaScript stack trace is accessible when JavaScript exceptions occur. However, acquiring a stack trace for custom exceptions thrown manually may require specific techniques.

For custom exceptions, the following script can be employed:

function stacktrace() { 
  function st2(f) {
    return !f ? [] : 
        st2(f.caller).concat([f.toString().split('(')[0].substring(9) + '(' + f.arguments.join(',') + ')']);
  }
  return st2(arguments.callee.caller);
}

Additionally, modern browsers offer the console.trace() function for capturing stack traces during debugging.

Updated Solution (2013):

A simpler approach is to utilize the Error object's stack property:

function stackTrace() {
    var err = new Error();
    return err.stack;
}

Das obige ist der detaillierte Inhalt vonWie erhalte ich Stack-Trace-Informationen in JavaScript?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn