This article describes the JavaScript implementation method to display the function call stack. Share it for your reference, as follows:
The function call relationship between many large JavaScript applications is very complex. During the development or debugging process, it is often necessary to track which function calls a function before it is triggered to execute. It is very important to understand the order of call of these functions for us to understand the data flow of the code.
Firebug provides console.trace() to display the function stack. Adding the following line of code to the place where debugging is needed can display the context relationship when the function is called. IE6 is not so convenient. It does not provide a tool to display the function stack. When it is inevitable to debug the code in IE6, use the following code to display the function stack (it is recommended to save the following JavaScript code as console.trace.js, and refer to the page by introducing js externally):
The JAVASCRIPT code is as follows:
/*** Get the function name** @param {Function} func Function reference* @return {String} Function name*/function getFunctionName(func) { if ( typeof func == 'function' || typeof func == 'object' ) { var name = ('' + func).match(/function/s*([/w/$]*)/s*/(/); } return name && name[1];}if (!('console' in window)) { window.console = {};}if (!console.trace) { /** * Show function stack<br/> * In order to be unified with Firebug, add the trace method to the console object* * @param {Function} func function reference* * @examplefunction a() {b();}function b() {c();}function c() {d();}function d() {console.trace();}a(); */ console.trace = function() { var stack = [], caller = arguments.callee.caller; while (caller) { stack.unshift(getFunctionName(caller)); caller = caller && caller.caller; } alert('functions on stack:' + '/n' + stack.join('/n')); }};For more information about JavaScript related content, please check out the topics of this site: "Summary of JavaScript switching effects and techniques", "Summary of JavaScript search algorithm skills", "Summary of JavaScript animation effects and techniques", "Summary of JavaScript errors and debugging techniques", "Summary of JavaScript data structures and algorithm skills", "Summary of JavaScript traversal algorithms and techniques", and "Summary of JavaScript mathematical operations usage"
I hope this article will be helpful to everyone's JavaScript programming.