// Here we will store the current stack trace into myObj Error.captureStackTrace(myObj); c(); } function a() { b(); } // First we will call these functions a(); // Now let's see what is the stack trace stored into myObj.stack console.log(myObj.stack); // This will print the following stack to the console: // at b (repl:3:7) < // at a (repl:2:1) // at repl:1:1 < // at realRunInThisContextScript (vm.js:22:35) // at sigintHandlersWrap (vm.js:98:12) // at ContextifyScript.Script.runInThisContext (vm.js:24:12) // at REPLServer.defaultEval (repl.js:313:29) // at bound (domain.js:280:14) // at REPLServer.runBound [as eval] (domain.js:293:12) // at REPLServer.onLine (repl.js:513:10) 大年夜膳绫擎的示例可以看出, 起首调用函数 a(被压入客栈), 然后在 a 琅绫擎调用函数 b(被压入客栈且在a之上), 然后在 b 中捕获到当前的客栈信息, 并将其存储到 myObj 中. 所以, 在控制台输出的客栈信息中仅包含了 a和 b 的调用信息.
如今, 我们给 Error.captureStackTrace 传递一个函数作为第二个参数, 看下输出信息:
- const myObj = {};
-
-
-
- function d() {
-
- // Here we will store the
推荐阅读
关于JavaScript的数组随机排序
JavaScript 开辟中有时会碰到要将一个数组随机排序(shuffle)的需求,一个常见的写法是如许:function shuffle(arr) { arr.sort(function () { return Math.random() - 0.5; >>>详细阅读
本文标题:JavaScript错误处理和堆栈追踪浅析
地址:http://www.17bianji.com/lsqh/34615.html
1/2 1