JavaScript implements a method of displaying function call stacks _javascript tips

Source: Internet
Author: User

The example in this article describes the method that JavaScript implements to display function call stacks. Share to everyone for your reference, specific as follows:

The function call relationship between many large JavaScript applications is very complex, and it is important to understand the order in which the functions are invoked before they are triggered, often in the process of development or debugging, to track which functions are invoked.

Firebug provides console.trace () to display the function stack, and the following line of code where you need to debug can show the context of the function call. IE6 is not so convenient, it does not provide a tool to display the stack of functions, when the inevitable need to debug the code under IE6, Use the following code to display the function stack (it is recommended that the following JavaScript code be saved as Console.trace.js and referenced to the page through the external introduction of JS):

The JavaScript code is as follows:

/** * Get function name * * @param {function} func function Reference * @return {String} function name */functions Getfuncti Onname (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 function Stack <br/> * For and firebug unification, TRAC The E method is added to the console object * @param {function} func functions reference * * @example function A () {B ();} function B () {C ();} func
   tion 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 '));

}
}; 

More readers interested in JavaScript-related content can view the site topics: "JavaScript switching effects and tips summary", "JavaScript Search Algorithm Skills Summary", "JavaScript animation effects and tips summary", " JavaScript error and debugging skills Summary, JavaScript data structure and algorithm skills summary, JavaScript traversal algorithm and skills summary and JavaScript mathematical calculation usage Summary

I hope this article will help you with JavaScript programming.

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.