Rewrite for…each with for…of #68

Merged
p3k merged 3 commits from rewrite-for-each-with-for-of into helma-🐜 2023-08-27 10:13:57 +00:00
p3k commented 2023-08-26 20:45:34 +00:00 (Migrated from github.com)

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:

💡 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);
}
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! ```js 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); } ```
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: antville/helma#68
No description provided.