作家
登录

深入理解JavaScript错误和堆栈追踪

作者: 来源: 2017-04-25 17:10:21 阅读 我要评论

至此你应当会问本身:“这到底有什么竽暌姑?”。这异常有效,因为你可以用它来隐蔽与用户无关的内部实现细节。在Chai中,我们应用它来避免向用户显示我们是若何实施检查和断言本身的不相干的细节。

操作客栈追踪拭魅战

  1. // `ssfi` stands for "start stack function". It is the reference to the 
  2. // starting point for removing irrelevant frames from the stack trace 
  3. function AssertionError (message, _props, ssf) { 
  4.   var extend = exclude('name''message''stack''constructor''toJSON'
  5.     , props = extend(_props || {}); 
  6.  
  7.   // Default values 
  8.   this.message = message || 'Unspecified AssertionError'
  9.   this.showDiff = false
  10.  
  11.   // Copy from properties 
  12.   for (var key in props) { 
  13.     this[key] = props[key]; 
  14.   } 
  15.  
  16.   // Here is what is relevant for us: 
  17.   // If a start stack function was provided we capture the current stack trace and pass 
  18.   // it to the `captureStackTrace` function so we can remove frames that come after it 
  19.   ssf = ssf || arguments.callee; 
  20.   if (ssf && Error.captureStackTrace) { 
  21.     Error.captureStackTrace(this, ssf); 
  22.   } else { 
  23.     // If no start stack function was provided we just use the original stack property 
  24.     try { 
  25.       throw new Error(); 
  26.     } catch(e) { 
  27.       this.stack = e.stack; 
  28.     } 
  29.   } 

如你所见,我们应用Error.captureStackTrace捕获客栈追踪并将它存储在我们正在创建的AssertError实例中(如不雅存在的话),然后我们将一个肇端客栈函数传递给它,以便大年夜客栈跟踪中删除不相干的调用帧,它只显示Chai的内部实现细节,最终使客栈变得清楚清楚明了。


  推荐阅读

  深度卷积神经网络在目标检测中的进展

近些年来,深度卷积神经收集(DCNN)在图像分类和辨认上取得了很明显的进步。回想大年夜 2014 到 2016 这两年多的时光,先后出现出了 R-CNN,Fast R-CNN, Faster R-CNN, ION, HyperNet, SD>>>详细阅读


本文标题:深入理解JavaScript错误和堆栈追踪

地址:http://www.17bianji.com/lsqh/34953.html

关键词: 探索发现

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

网友点评
自媒体专栏

评论

热度

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