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

后端 未结 3 579
时光说笑
时光说笑 2021-01-31 08:34

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
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-31 08:56

    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.

提交回复
热议问题