作家
登录

JavaScript错误处理和堆栈追踪浅析

作者: 来源: 2017-04-07 09:18:02 阅读 我要评论

  •  
  •     // 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) <-- Since it was called inside B, the B call is the last entry in the stack 
  •  
  • //    at a (repl:2:1) 
  •  
  • //    at repl:1:1 <-- Node internals below this line 
  •  
  • //    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 传递一个函数作为第二个参数, 看下输出信息:

    1. const myObj = {}; 
    2.  
    3.   
    4.  
    5. function d() { 
    6.  
    7.     // 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

    关键词: 探索发现

    乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

    网友点评
    自媒体专栏

    评论

    热度

    精彩导读
    栏目ID=71的表不存在(操作类型=0)