"Go" shows how the JavaScript function calls the stack

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.