Javascript 为什么 ECMAScript 5 中的 Object 没有 forEach 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14929819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is there no forEach method on Object in ECMAScript 5?
提问by Samuel Rossille
ECMAScript 5's array.forEach(callback[, thisArg])is very convenient to iterate on an array and has many advantage over the syntax with a for:
ECMAScript 5array.forEach(callback[, thisArg])非常方便地迭代数组,并且比使用 for 的语法有很多优势:
- It's more concise.
- It doesn't create variables that we need only for the purpose of iterating.
- It's creates a visibility scope for the local variables of the loop.
- It boosts the performance.
- 它更简洁。
- 它不会创建仅用于迭代目的的变量。
- 它为循环的局部变量创建了一个可见范围。
- 它提高了性能。
Is there a reason why there is no object.forEachto replace for(var key in object)?
有没有理由不object.forEach更换for(var key in object)?
Of course, we could use a JavaScript implementation, like _.each or $.each but those are performance killers.
当然,我们可以使用 JavaScript 实现,比如 _.each 或 $.each,但这些都是性能杀手。
回答by Alex Wayne
Well, it's pretty easy to rig up yourself. Why further pollute the prototypes?
好吧,装备自己很容易。为什么要进一步污染原型?
Object.keys(obj).forEach(function(key) {
var value = obj[key];
});
I think a big reason is that the powers that be want to avoid adding built in properties to Object. Objects are the building blocks of everything in Javascript, but are also the generic key/value store in the language. Adding new properties to Objectwould conflict with property names that your Javascript program might want to use. So adding built in names to Objectis done with extreme caution.
我认为一个重要的原因是想要避免将内置属性添加到Object. Objects 是 Javascript 中一切的构建块,但也是该语言中的通用键/值存储。添加新属性Object会与您的 Javascript 程序可能想要使用的属性名称冲突。因此,添加内置名称时要Object格外小心。
Array is indexed by integers, so it doesn't have this issue.
数组由整数索引,所以它没有这个问题。
This is also why we have Object.keys(obj)instead of simply obj.keys. Pollute the Objectconstructor as that it typically not a big deal, but leave instances alone.
这也是为什么我们有Object.keys(obj)而不是简单的obj.keys. 污染Object构造函数,因为它通常没什么大不了的,但不要管实例。
回答by Norguard
.forEachdoescreate variables just for the purpose of iteration... Just not ones you create, yourself.
If you're using a shim, then they're JS variables in a closure, else they're likely in C++, or whatever the browser's native implementation is.ES6 is getting
for ... ofwhich will iterate through whatever you pass it.ES6 is also getting block-level variables (rather than function-scoped), using
let, instead ofvarYou will be able to add iterators to specific kinds of objects in ES6 (ie: the objects you build to support iterators).
.forEach确实创建变量只是为了迭代......不是你自己创建的。
如果您使用的是 shim,那么它们是闭包中的 JS 变量,否则它们可能是 C++ 或任何浏览器的本机实现。ES6 正在获取
for ... of它将遍历您传递的任何内容。ES6 还获取块级变量(而不是函数作用域),使用
let, 而不是var您将能够向 ES6 中的特定类型的对象添加迭代器(即:您构建以支持迭代器的对象)。
But the problem here is that ES5 was meant to preserve most of the syntax of ES3 (few new keywords, and no new keywords in regular-use).
Most of the additional functionality had to be method calls (prototyped, typically).
And arrays needed plenty of work, given how much more use JS has seen since ES3 launched.
但这里的问题是 ES5 旨在保留 ES3 的大部分语法(很少有新关键字,并且在常规使用中没有新关键字)。
大多数附加功能必须是方法调用(通常是原型)。
考虑到自 ES3 发布以来 JS 的使用量增加了多少,数组需要大量的工作。
Objects don't need a .map()/ .reduce()ability, unless you're building VERY SPECIFIC objects, in which case, you're implementing those yourself.
对象不需要.map()/.reduce()能力,除非您正在构建非常特定的对象,在这种情况下,您正在自己实现这些对象。
回答by Varun Achar
You have to remember that every object in javascript inherits from Object. Not every object in javascript needs to have it's properties iterated. Those are mostly confined to data model objects. Adding the forEach method will only bulk up the prototype of everyobject unnecessarily.
您必须记住,javascript 中的每个对象都继承自Object. 并非 javascript 中的每个对象都需要迭代其属性。这些主要限于数据模型对象。添加 forEach 方法只会不必要地增加每个对象的原型。
If you currently see the Object and it's methods, they are present only to identify the object, or to distinguish one object from another.
如果您当前看到 Object 及其方法,它们的存在只是为了识别对象,或将一个对象与另一个对象区分开来。

