我正在开发 Angular 应用程序,其中我使用 angular.forEach
方法遍历数组(不是对象)。它的工作方式与 Array.prototype.forEach
类似。所以我对使用它有一些疑问。
- 哪种方法适合按照标准使用。
- 就性能而言,这是不错的。我在 jsPerf 上测试过, 它给出了相同的结果。
最佳答案
根据angular.forEach-Documentation ,它确实可以防止 TypeError(s)
。
Unlike ES262's Array.prototype.forEach, Providing 'undefined' or 'null' values for obj will not throw a TypeError, but rather just return the value provided.
所以它几乎就是图书馆一直在做的事情。它包装了一些功能,如检查 null、undefined 等并处理这些事件,因此开发人员不必处理它。
如果您想自己处理那些TypeError
,请使用Array.prototype.forEach
,否则请使用库函数。
在性能方面不会有什么不同。
关于javascript - 在 Angular 应用程序中使用 angular.forEach() 和 Array.prototype.forEach(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32306331/