js -version 170 in Ubuntu

Calling js -version 170 on the command line in Ubuntu does not seem to allow the new functionality, i.e. from the examples:

{{ syntax.JavaScript{code: "var ten_squares = [i * i for each (i in range(0, 10))];"} }}

This array comprehension does not work, namely because range is said to not be defined.

Spent Iterators Should Not Throw an Exception

Exceptions should be used when there is a runtime error, not when a normal termination condition occurs. When calling it.next() on an iterator, the return value of that call should indicate the status of the iterator. The value returned should probably be null, but I'd be willing to entertain arguments for false, undefined, or something of the sort.

Throwing an exception to handle a normal case like a spent iterator will cause developers to litter code with try/catch blocks and create an enormous amount of unnecessary complexity. Code like the following should be possible in the spirit of JS and other languages that support similar constructs:

while ((item = it.next()) !== null)

h3h 16:56, 1 June 2006 (PDT)

The problem with returning a special value is that whatever value you choose can't be produced by the iterator.

The design of this feature is very similar to Python's, and the same discussion happened between the Python developers in 2001. They reached the conclusion that an exception is the best way to handle termination of an iterator. See python-dev threads such as "why not "return StopIteration"?", which quotes from the Python Enhancement Proposal that introduced iterators, PEP 234.

Experience with this design in Python has shown that developers don't often have to explicitly call the next method or catch the exception; iterators are usually iterated over in for loops, or passed to functions. Your example would usually be written in JS as

for (var item in it) {

or even


Deltab 08:48, 30 June 2006 (PDT)

I was looking at the yield statement, and it reminded me of the lack of a sleep/suspend/wait function, which makes a lot of code much more painful than it needs to be. Are there any plans to add this kind of function to 1.7?

It would be especially nice if there were a WaitFor(evt) function and a corresponding NotifyThat(evtHappened) so that threads could simply wait until another thread (or the human user!) was finished.

(Apologies if this is the wrong place to raise this.)

--Voracity 00:45, 25 July 2006 (PDT)

Bug in "Row capture and record updating" example?

The final example, it's not clear how newPersonRecord could include the property "name", as "name" was never a property of z (from the previous example).

Should the example use personRecord instead of z?

Tag 22:07, 26 July 2006 (PDT)

Syntax for replacing native iterators?

If you wish to provide a custom iterator, you should override the getter for __iterator__ to return an instance of your custom iterator.

I'm having trouble finding the precise syntax to override these native iterators.

Let's say I want to rewrite the DOM HTMLCollection iterator to return only nodes and ignore the builtin properties (length, item, and namedItem). My code looks something like this:

    var DOMIterator = function(nodeList) {
      var len  = nodeList.length, i = 0, ret;
      for (i = 0; i < len; i++) {
        yield [i, nodeList[i]];
      throw StopIteration;
    var nodes = document.getElementsByTagName('p');
    nodes.__defineGetter__("__iterator__", function() {
      return new DOMIterator(this);

When I evaluate nodes.__iterator__, I get {{ mediawiki.external('object Generator') }}. But when I try for (var x in nodes) { /* do something */ }, I get a TypeError ("nodes has invalid __iterator__ value ({})").

I'm sure I've overlooked something simple, but I've tried a crapload of variations on this theme — making the iterator return a function, removing the yield statement and defining an ordinary next method, etc. What am I doing wrong? Savetheclocktower 11:46, 29 July 2006 (PDT)

See --Nickolay 16:45, 29 July 2006 (PDT)
Your problem is that you used __defineGetter__ to set the value of nodes.__iterator__ to be new DOMIterator(this) (incidentally, I don't think the new is needed there, and I'm not even sure what the new operator applied to a function which defines a generator like that even means).
The __iterator__ property should be a reference to a function which returns DOMIterator(this). This is somewhat counterintuitive (or at least it was to me when I was first playing around with this), but consider it this way: if __iterator__ were actually a Generator, that would mean you could only iterate over an object once. __iterator__ needs to be implemented via a getter-style method, and since __defineGetter__ is Mozilla-specific, that method of implementation's out, and thus only a standard JS function will do.
As for iterating over only the items in an HTMLCollection, I believe there's a bug that keeps this from working correctly (which is demonstrated as a reduced testcase in the newsgroup post mentioned above); see my response to that post, as well. --Waldo 02:24, 30 July 2006 (PDT)
Cool, thanks. So should that passage read "you should override the __iterator__ property to return an instance..."? I don't understand why it's referred to as a getter in that context.
At any rate, I'd stumbled upon the correct method for this a while back, but didn't realize it because of the bug you mention -- it was iterating over my generator and all remaining properties, so I couldn't distinguish it from an ordinary for..in. Savetheclocktower 16:33, 31 July 2006 (PDT)

On generators and iterators

Some useful links:

--Nickolay 07:42, 30 July 2006 (PDT)

Ignoring some returned values

I got email from Brendan last night indicating this is in fact supported after all in JavaScript 1.7, so I've restored this section to the documentation.--Sheppy 12:42, 31 October 2006 (PST)

Reserved Words

It appears that handling of reserved words is different between 1.6 and 1.7. I developed an application that used an RPC object (using the Dojo toolkit) that had the delete method, it worked fine in FF2 (using 1.7) but caused a Syntax Error in FF1.5 (using 1.6). delete is of course a reserved word. Changing the method name to "remove" and it works just fine. Just posting this as an advisory in case anyone is facing a similar issue MrWeeble 07:38, 1 December 2006 (PST)

An example of let constructs

I added another example of let definitions. I hope that this example explains part of the reasons let constructs are useful. Feel free to edit it. To avoid any doubts of copyright infringement, I copied the code from my own web page http://homepage2.nifty.com/polytope/javascript-let/ (in Japanese). --fcp 16:36, 1 June 2007 (PDT)

for each on arrays with destructuring assignment still unsafe

Other pages in this wiki discussing for each repeatedly state using it on an Array is unsafe because it picks up additions to Array.prototype (which even a file in the Firefox 2.0.x source tree (look for lang.js iirc) messes with!). Because this page does not have that warning I assumed this style of looping was now "safe". This does not seem to be the case:

   Array.prototype.spork = 42;
   Array.prototype.owner = {name: 'us'};
   var people = [{name: 'me'}];
   for each (var {name: n} in people) {


produces "me", "undefined", "us" ("undefined" because (42).name is undefined), which I still think is surprising behaviour. However I am pretty much a javascript newbie, so I think I should not add this to the actual page unless others agree. Marienz 09:31, 27 August 2007 (PDT)

Yep, this could be mentioned. --Nickolay 22:28, 8 September 2007 (PDT)

Another let (and destructuring assignment) example

I agree with the JavaScript_style_guide that it is useful to "wrap functions and variables inside an object with a unique name to avoid conflicting with existing or future function and variable names", but having to refer to the name of that wrapper object all over the place is annoying (since it should have a nontrivial name to be of use for preventing collisions). It helps if you can refer to this instead of the full name, but that does not work from event handlers unless you do something like:

// This is not real-world code, obviously :P
var ReallyLongNonClashingName = {
    data: 1,

    onClick: function(evt) {

// but this is (except for the ReallyLongNonClashingName)
for (let [name, value] in ReallyLongNonClashingName) {
    if (typeof(value) == 'function') {
        let original = value;
        ReallyLongNonClashingName[name] = function(/* *args */) {
            return original.apply(ReallyLongNonClashingName, arguments);

and now both something.addEventListener('onclick', ReallyLongNonClashingName.onClick, false) and something.removeEventListener('onClick', ReallyLongNonClashingName.onClick, false) work (and you can use this.onclick instead if it's another member doing the (un)registering). The way this uses let is basically the same as the other example (the one with the alert(i) handlers), so perhaps there is a better place to put this, just not sure where. Feel free to copy over to a code snippets page or something if it's not appropriate here :). Marienz 09:57, 27 August 2007 (PDT)

FWIW, a more common solution is passing anonymous function wrappers as the event handlers, i.e. smth.onclick = function(e) { return MyObject.onClick(e); }. --Nickolay 22:28, 8 September 2007 (PDT)

What does "completion value" mean?

Hello. I am a Japanese MDC user, and now I'm translating this article into Japanese ja:New in JavaScript 1.7. But I don't understand the meaning of the sentence "The completion value of the let statement is the completion value of the block." in New in JavaScript 1.7#let statement. What does "completion value" mean? --Yuichirou 07:01, 20 September 2007 (PDT)

Sheppy says this was copied from the text in specification and neither he nor me know what this is supposed to mean. I suggest removing this text from the article and asking for clarification in dev-tech-js-engine. --Nickolay 08:06, 24 September 2007 (PDT)
Oh, really... I'd like to ask, but considering my English, it's too hard for me. If possible, I'd like you or other English speakers to do so and rewrite this article. It's good for other language MDC users, not only me. --Yuichirou 19:48, 24 September 2007 (PDT)
OK, posted this question in dev-tech-js-engine: . --Nickolay 07:37, 25 September 2007 (PDT)
"Completion value" refers to the state-machine result of a computation: what value, if any, is returned, where execution next progresses, etc. Basically what it means is that continue/break/throw work the same way they do in normal code, more or less. There's nothing especially important about these details to someone writing JS, because intuition serves well enough, so I slightly tweaked it to remove this sentence. --Waldo 08:37, 25 September 2007 (PDT)

JS 1.7 in XBL?

How does one enable JS 1.7 in XBL? Doesn't seem to work. I've used the proper attribute for script, but doesn't seem to work in XBL. Niczar 05:36, 24 September 2007 (PDT)

I think you can't. Could you ask in dev-tech-xbl (mozilla.dev.tech.xbl) and check for / file a bug if you don't get a response? --Nickolay 08:13, 24 September 2007 (PDT)

Iterator() described incorrectly

The article claims:

The default iterator yields the object's properties, according to the usual for...in and for each...in model.

But in my tests, the default iterator does not yield properties on the object's prototype chain, while the for loops do. This is a pretty major difference. It means I could add all kinds of prototype functions and not worry about them showing up during iteration, as long as I use Iterator() when iterating. HeroreV 15:12, 11 February 2008 (PST)

Problem on Let documentation

I wasn't the one who pasted this code segment, but I think it is worth adding to the talk page because I get the same contrary result as the commenter:

"I copied and pasted this code into an xhtml page <script type="text/javascript;version=1.7">    var x = 'global';     let x = 42;     document.write(x + "<br>\n");  </script> and the output was "42" . I'm not sure "let" works correctly here. Please check. (Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv: Gecko/2008092417 Firefox/3.0.3)"