For undefined, it's the void operator, which, given any argument, produces the value that undefined is initially bound to. Conventionally, people write void(0).
In JavaScript 1.7 and later, there's a special constant called StopIteration that's thrown after an iterator runs out of values. This too is a mutable global. But this value is a little trickier to get at reliably. Here's the simplest expression I could come up with that produces the StopIteration value:
The inner function is a generator by virtue of textually containing the yield keyword, but it returns immediately, yielding no values, because of the short-circuited logical or. (Note that because of yield's finicky grammar, it has to be parenthesized.) So by calling next() on it, it immediately raises StopIteration, which the outer function catches and returns.(function(){try{(function(){true||(yield)})().next()}catch(e){return e}})()
2 comments:
Ha! I didn't know that about Javascript. It reminds me of really old versions of Fortran (which I'm of course not old enough to have actually programmed in) where numerical constants were mutable globals. I love the idea of assign a different value to 35. Cause seriously, who needs 35?
mike machenry said:
Cause seriously, who needs 35?
Isn't your birthday coming up?
-- Paul
Post a Comment