methods for displaying JavaScript function call stacks
The function call relationships among many large JavaScript applications are very complex,
During development or debugging, it is often necessary to keep track of which function calls a function before triggering execution, and it is important to understand the order in which these functions are called for the data flow of the code.
Firebug and Chrome provide console.trace () to display the function stack, and the following line of code, where it needs to be debugged, shows the context of the function call.
IE6 is not so convenient, it does not provide a tool to display the function stack, when it is unavoidable to debug the code under IE6, use the following code to display the function stack
(We recommend that you save the following JavaScript code as console.trace.js, referring to the page by introducing JS externally):
The 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) {
/**
* Show function Stacks <br/>
* To unify with Firebug, add the trace method to the console object
* @param {Function} func function reference
* @example
function 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 '));
}
};
Article Source: West Wind Thin horse released in June, http://cshbbrain.iteye.com/blog/1833461
"Go" shows how the JavaScript function calls the stack