icey-t
icey-t

Reputation: 303

Why do objects use `Symbol.iterator`, instead of just `iterator`

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.

Upvotes: 0

Views: 51

Answers (1)

Roman Odaisky
Roman Odaisky

Reputation: 2941

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.

Upvotes: 3

Related Questions