Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Introducing ES2015 Objects and New Collection Types For...Of

What does he mean by "regular objects"? (2:50)

At 2:50 he mentions that the For...Of loop will not work with regular objects. Not sure what this distinction is.

2 Answers

Hi Rick!

I think for the purposes of understanding the video is that for/of loops only work with iterable objects (such as an array).

Therefore, in this context, his reference to "regular objects" would really mean any non-iterable object.

More info:

21.3.8 Plain objects are not iterable 
Plain objects (as created by object literals) are not iterable:

for (const x of {}) { // TypeError
    console.log(x);
}
Why arenโ€™t objects iterable over properties, by default? The reasoning is as follows. There are two levels at which you can iterate in JavaScript:

The program level: iterating over properties means examining the structure of the program.
The data level: iterating over a data structure means examining the data managed by the program.
Making iteration over properties the default would mean mixing those levels, which would have two disadvantages:

You canโ€™t iterate over the properties of data structures.
Once you iterate over the properties of an object, turning that object into a data structure would break your code.
If engines were to implement iterability via a method Object.prototype[Symbol.iterator]() then there would be an additional caveat: Objects created via Object.create(null) wouldnโ€™t be iterable, because Object.prototype is not in their prototype chain.

It is important to remember that iterating over the properties of an object is mainly interesting if you use objects as Maps1. But we only do that in ES5 because we have no better alternative. In ECMAScript 6, we have the built-in data structure Map.

That text can be found here:

https://exploringjs.com/es6/ch_iteration.html#sec_iterable-data-sources

Also:

https://www.xspdf.com/resolution/58815847.html

I hope that helps.

Stay safe and happy coding!

Thanks, Peter, for the answer and the sources. Just what I was looking for. I had googled "regular" and still didn't have much luck. I guess "plain" would have been the word. I take notes on all the treehouse and textbooks I study and didn't find anything defining "regular" objects in my notes either, except as used in comparison with JSON objects. So I was left in doubt about whether this term "regular objects" was some nomenclature I should be familiar with. Your answer hit the mark!