[Pas2js] for .. in loop
Michael Van Canneyt
michael at freepascal.org
Fri May 18 17:44:04 CEST 2018
On Fri, 18 May 2018, Mattias Gaertner wrote:
>> 2) Why doesn’t this work with JSArray like you would expect? I mean if a function returns a JSArray of integers for example can’t I use for..in and have the iterator be of type integer? That’s really the question I’m asking.
>
> Expectation is the god of many faces.
>
> Keep in mind, that a JS array is still an associated array:
>
> var a = [1,2];
> a.bla = 'foo';
> for (var key in a){
> console.log(a[key]);
> }
>
> gives:
> 1
> 2
> foo
>
> TJSArray redefines the default property, so that A[index] only takes
> integers as index. Same for the typedarrays.
> It would be nice, if these types could redefine the default enumerator
> too. For example enumerating a TJSInt8Array would iterate from 0 to
> length-1 giving Shortints.
>
> Maybe if an external class redefines the default property and has a
> property length:integer.
>
> Michael, what do you think?
I'm a bit wary of introducing gimmicks like this.
I would rather wait for proper enumerator support.
What we could do is implement as you propose, and later, when proper
enumerators are implemented, the above can be a default in case there is no
explicit enumerator.
Michael.
More information about the Pas2js
mailing list