0

Do objects use the property Symbol.iterator, instead of just iterator, to prevent us from overwriting the iterator property on the Object prototype?

If so, are there any other reasons?

Just trying to understand symbols in JS.

icey-t
  • 283
  • 3
  • 11
  • 3
    Pretty good discussion and links here: https://stackoverflow.com/questions/21724326/what-is-the-motivation-for-bringing-symbols-to-es6 – Mark Dec 22 '18 at 00:45

1 Answers1

2

If it were just iterator, it could break old code that had objects with methods called iterator. Likewise, whenever the designers of JS want to let developers add other special methods to their objects, they would run the risk of clobbering existing names and breaking existing code.

An alternative is Python’s __double_underscore__ convention where developers are not supposed to defined methods named like this.

Roman Odaisky
  • 2,811
  • 22
  • 26