Green Grasso Holm
Green Grasso Holm

Reputation: 704

Why doesn't assignment to Symbol.iterator throw an error?

In Chrome on Windows, I have this in the console:

> Symbol.iterator
< Symbol(Symbol.iterator)
> Symbol.iterator = "Hello!";
< "Hello!"
> Symbol.iterator;
< Symbol(Symbol.iterator)

So Symbol.iterator (thankfully) doesn't change, but why doesn't my attempt to assign to it throw an error?

Upvotes: 1

Views: 63

Answers (1)

Bergi
Bergi

Reputation: 665410

In sloppy mode, the failed assignment doesn't cause an exception. Use strict mode instead:

(function(){ "use strict"; Symbol.iterator = "Hello" }())

Uncaught TypeError: Cannot assign to read only property iterator of function function Symbol() { [native code] }

Upvotes: 2

Related Questions