2

I'm reading Eloquent JavaScript's "The Iterator Interface" section and I have trouble understanding why we need to define MatrixIterator function to iterate Matrix's contents. I usually use for inside of another for to iterate the contents of a two-dimensional matrix, similar to this. Is it because the [Symbol.iterator]'s of each object in JS is used whenever you call the shorthand of for loop which is for...of?

Furthermore, am I correct to assume that the standard [Symbol.iterator] cannot automatically iterate a two-dimensional objects and thus, the need to create MatrixIterator and assigning it as:

Matrix.prototype[Symbol.iterator] = function() {
  return new MatrixIterator(this);
};

Are we allowed to do this instead: Matrix.prototype[Symbol.iterator] = MatrixIterator;?

Off-topic: I feel like I should know some other things more in-depth as I feel rather confused about these concepts. Could someone also elaborate what interface means? The book did mention it as:

Different pieces of such a program interact with each other through interfaces, limited sets of functions or bindings that provide useful functionality at a more abstract level, hiding their precise implementation.

and

Separating interface from implementation is a great idea. It is usually called encapsulation.

but it did not mention what implementation is.

Richard
  • 7,037
  • 2
  • 23
  • 76
  • 1
    Voted to close because this is 3-4 questions at once... First, yes, a `for` loop does not know how to iterate on a `Matrix`, so you need a custom `Symbol.iterator`. This is kind of a silly toy example though, usually with matrices you want to multiply or do other operations on the whole matrix. – We Are All Monica Aug 21 '18 at 05:34
  • Are we allowed to do this instead: `Matrix.prototype[Symbol.iterator] = MatrixIterator` - This won't work because the `MatrixIterator` no longer has a reference to the matrix object (the `this` in `new MatrixIterator(this)`). It might be possible to write `MatrixIterator` in a way so that this can work, but it wouldn't be as clean or as useful code. – We Are All Monica Aug 21 '18 at 05:35
  • I see, thank you for your explanation! @jnylen – Richard Aug 21 '18 at 09:11

1 Answers1

2

the shorthand of for loop which is for...of?

No, for … of is not a shorthand for a normal for (…; …; …) loop. It's a completely separate mechanism.

Why do we need to define MatrixIterator function to iterate Matrix's contents?. Is it because the [Symbol.iterator]'s of each object in JS is used whenever you use for...of?

Yes. We define the MatrixIterator as it conforms to the iterator interface, as is expected to be returned by a Symbol.iterator method to be usable in such a loop.

Of course there are alternative ways to achieve this, we don't necessarily need to make an extra MatrixIterator class. A generator function is usually the easiest.

I usually use for inside of another for to iterate the contents of a two-dimensional matrix

Sure, but that's quite some syntactic overhead - two loops, two counters, double indentation. It could be much simpler, and we don't want to repeat this pattern everywhere when we want to iterate a matrix. The iterable interface allows this.

am I correct to assume that the standard [Symbol.iterator] cannot automatically iterate a two-dimensional objects

There is no standard Symbol.iterator method. Every type needs to define it itself. Array.prototype does, for example, but it works only on arrays not on our Matrix class.

Are we allowed to do this instead: Matrix.prototype[Symbol.iterator] = MatrixIterator;?

That doesn't work, as MatrixIterator a) is a constructor that needs to be invoked with new b) takes the matrix instance that it should iterate as an argument

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • I see, thank you for the answers. I think that I'm missing a lot of things I should've known by this point, do you think so too? If so, what should I read or focus on? – Richard Aug 21 '18 at 09:11
  • 1
    @WealthyPlayer No idea, I think "Eloquent JavaScript"s curriculum is just fine. (At least that of the first edition seemed reasonable, I didn't read the second one) – Bergi Aug 21 '18 at 09:29