antville / helma

Antville Fork of Helma Object Publisher
https://antville.org
Other
3 stars 1 forks source link

Rewrite `for…each` with `for…of` #68

Closed p3k closed 10 months ago

p3k commented 10 months ago

Rhino’s for…each loop is a non-standard JavaScript extension and causing some problems, e.g. with JSDoc.

Rewriting it is quite straight-forward, either by using the for…of loop, the Array.forEach() method, or the good old for…in syntax:

:bulb: For the for…of loop to work, the VERSION_ES6 flag must be enabled in Rhino!

var collection = [1, 2, 3];

// Rhino-specific for…each loop
for each (let value in collection) {
  console.log(value);
}

// for…of loop
for (let value of collection) {
  console.log(value);
}

// Array.forEach method
collection.forEach(value => console.log(value));

collection = {a: 1, b: 2, c: 3};

// for…in loop – Rhino does not support `Object.values()`
for (let key in collection) {
  let value = collection[key];
  console.log(value);
}