How to display function call stacks using JavaScript _ javascript skills

Source: Internet
Author: User
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 '));}};

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.