Allows javascript objects to traverse

will there be any negative impact if I use the generator function to make the Object prototype traverable?
the code is as follows

Object.prototype[Symbol.iterator] = function* () {
  for(let value in this){
    yield this[value]
  }
}

in fact, the main question is what negative impact it will have, that is, why it is not recommended to modify it, rather than looking for alternatives

Apr.09,2022

it is recommended not to modify the prototype to understand lodash.js


it is better to use Map instead of


generally traverse objects. You can traverse the resulting keys array through Object.keys () .

it's just that the order of the keys fetched each time may be inconsistent, because the key-value pair itself is unordered.

The traversal of

Object is meaningless because Object is unordered.
you can use the Object.keys Object.values methods to get traversal results of keys and values, but the order returned after each execution is not certain, but the key values correspond.

Menu