如何利用JavaScript的arguments对象实现用代码打印调用栈的需求

示例代码:


<html>
<script>
function getCallStack() {
    var stack = "Callstack:", fn =arguments.callee;
    while ( (fn = fn.caller) ) {
        stack = stack + "\n" +fn.name;
    }
    return stack;
}

function test1() {
    console.log(getCallStack());
}
function test2() {
    test1();
}
function test3() {
    test2();
}
function test4() {
    test3();
}
test4();
</script>
</html>

发布了7169 篇原创文章 · 获赞 654 · 访问量 124万+

猜你喜欢

转载自blog.csdn.net/i042416/article/details/105086659