This article mainly introduces how to display the function call stack in JavaScript, and analyzes the specific functions and usage of the function call stack in JavaScript, for more information about how to use JavaScript to display the function call stack, see the following example. We will share this with you for your reference. The details are as follows:
The function call relationships between many large JavaScript applications are very complex. During development or debugging, it is often necessary to track which functions call a function before execution is triggered, figuring out the call sequence of these functions is very important for us to understand the data flow of the Code.
Firebug provides console. trace () to display the function stack. Add the following line of code to debug the function to display the context of the function call. IE6 is not so convenient. It does not provide a tool for displaying function stacks. When it is inevitable to debug code under IE6, use the following code to display the function stack (we recommend that you save the following JavaScript code as the console. trace. js, which is referenced to the page by external introduction of js ):
The JAVASCRIPT code is as follows:
/*** Get 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) {/*** display the function Stack
* To be consistent 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 '));}};