javascript - 在 Angular 应用程序中使用 angular.forEach() 和 Array.prototype.forEach()

标签 javascript arrays angularjs foreach

我正在开发 Angular 应用程序,其中我使用 angular.forEach 方法遍历数组(不是对象)。它的工作方式与 Array.prototype.forEach 类似。所以我对使用它有一些疑问。

  1. 哪种方法适合按照标准使用。
  2. 就性能而言,这是不错的。我在 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/

相关文章:

javascript - 是否可以在 AngularJS 中的资源上设置默认查询参数?

javascript - 如何将外部库导入到 ReactJS 应用程序中(本地)

javascript - Node.js dgram 绑定(bind)在 udp6 和 udp4 上抛出 EADDRINUSE

php - 使用 foreach 访问数组元素

c++ - vector 数组 C++ - 添加元素时的奇怪行为

angularjs - 如何在 AngularJS 1.2 中获取 HTTP 响应状态代码

javascript - 如何更改 Titanium 中图像的颜色?

javascript - 实现路由以显示某些 View 的最简单方法是什么?

c++ - 在不使用单独的 typedef 的情况下声明函数指针数组的语法是什么?

javascript - 在 Angular ng-grid 中如何指定子数组的计数