Javascript 为什么像angular这样的项目有自己版本的通用函数?
我试图更好地理解或更新我在这方面的逻辑-例如,在angular中,它有angular.forEach() 我认为这是因为控制器(或一般模块)中的代码无法访问浏览器api(函数和对象等),因此浏览器的forEach函数也是如此 但我只是测试了一下,因为我试图更好地理解它/证明其基本原理-这两个console.log()表达式都起作用Javascript 为什么像angular这样的项目有自己版本的通用函数?,javascript,angularjs,Javascript,Angularjs,我试图更好地理解或更新我在这方面的逻辑-例如,在angular中,它有angular.forEach() 我认为这是因为控制器(或一般模块)中的代码无法访问浏览器api(函数和对象等),因此浏览器的forEach函数也是如此 但我只是测试了一下,因为我试图更好地理解它/证明其基本原理-这两个console.log()表达式都起作用 angular.module('myApp', []) .controller('JCtrl', ['$scope', function($scope) {
angular.module('myApp', [])
.controller('JCtrl', ['$scope', function($scope) {
$scope.test = 'scope and binding works';
[0, 1, 4].forEach(function(value) {
console.log(value);
});
console.log([].forEach);
}]);
这是一个通常用于优化、标准化和向后兼容性的api,它们提供了一个api来迭代一个“在任何环境下”都能工作的循环,因此您不必依赖其他框架来实现这一点
在您的示例中,forEach(如果我没有弄错的话,IE8不支持forEach),所以不要使用外部库或其他什么。您可以使用angulars尝试在IE8中使用
Array.forEach
——它不起作用
Javascript框架通常有两个主要用途:
var arr = [1,2,3,4];
if(arr.forEach)
{
arr.forEach(function(v,i) {
// do domething
})
}
else
{
for(var i = 0; i < arr.length; i++) {
var val = arr[i];
// do something
}
}
事实上,在内部,许多框架将检查内置方法是否存在并使用它,并且仅在需要时使用其自定义实现
第1点也与此相关,例如
Array.forEach
仅适用于数组,而Angular.forEach
将以相同的方式迭代数组和对象属性,因此,它为基础Javascript实现提供了额外的功能。直接从Angular.js的github源代码可以找到
所以,正如你所看到的
与ES262不同,为obj
提供“未定义”或“空”值不会引发TypeError,而只是返回提供的值
这样做是因为当你设计自己的系统时,很可能你需要的东西比原来的要多 因为它们包含像$digest()这样的角度组件,而标准javascript不会包含这些组件。与$timeout、$interval等相同。我认为答案有两个:一,并非所有浏览器都支持所有函数,如果这些函数在库中以任何频率使用,则有必要对其进行填充;第二,没有人愿意维护对其他库的依赖性。每个人都可以使用(比如)underline.js来polyfill
Array.prototype.forEach
,但是他们必须跟踪下划线库并相应地处理问题。从维护人员的角度来看,只在每个库中维护它要简单得多。在这种情况下,它不适用于每个循环的Angular,但从广义上讲,许多库将扩展开箱即用的行为。对于许多实现,您可以在回调中返回
,退出循环-类似于经典For
控制结构中的中断
和继续
。给出了很多很好的澄清和答案-谢谢-我选择了这个-因为它帮助我找到了与角度相关的答案源代码中的项目。
var arr = [1,2,3,4];
angular.forEach(arr, function(value, key) {
// do something
});
/**
* @ngdoc function
* @name angular.forEach
* @module ng
* @kind function
*
* @description
* Invokes the `iterator` function once for each item in `obj` collection, which can be either an
* object or an array. The `iterator` function is invoked with `iterator(value, key, obj)`, where `value`
* is the value of an object property or an array element, `key` is the object property key or
* array element index and obj is the `obj` itself. Specifying a `context` for the function is optional.
*
* It is worth noting that `.forEach` does not iterate over inherited properties because it filters
* using the `hasOwnProperty` method.
*
* Unlike ES262's
* [Array.prototype.forEach](http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.18),
* Providing 'undefined' or 'null' values for `obj` will not throw a TypeError, but rather just
* return the value provided.
*
```js
var values = {name: 'misko', gender: 'male'};
var log = [];
angular.forEach(values, function(value, key) {
this.push(key + ': ' + value);
}, log);
expect(log).toEqual(['name: misko', 'gender: male']);
```
*
* @param {Object|Array} obj Object to iterate over.
* @param {Function} iterator Iterator function.
* @param {Object=} context Object to become context (`this`) for the iterator function.
* @returns {Object|Array} Reference to `obj`.
*/