Why is there no forEach method on Object in ECMAScript 5?

妖精的绣舞 提交于 2019-12-03 03:31:12

问题


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:

  • 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.forEach to replace for(var key in object) ?

Of course, we could use a JavaScript implementation, like _.each or $.each but those are performance killers.


回答1:


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 Object would conflict with property names that your Javascript program might want to use. So adding built in names to Object is done with extreme caution.

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 Object constructor as that it typically not a big deal, but leave instances alone.




回答2:


  1. .forEach does create 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.

  2. ES6 is getting for ... of which will iterate through whatever you pass it.

  3. ES6 is also getting block-level variables (rather than function-scoped), using let, instead of var

  4. You will be able to add iterators to specific kinds of objects in ES6 (ie: the objects you build to support iterators).

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.

Objects don't need a .map() / .reduce() ability, unless you're building VERY SPECIFIC objects, in which case, you're implementing those yourself.




回答3:


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 every object unnecessarily.

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.



来源:https://stackoverflow.com/questions/14929819/why-is-there-no-foreach-method-on-object-in-ecmascript-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!