[Pas2js] for .. in loop
Mattias Gaertner
nc-gaertnma at netcologne.de
Fri May 18 17:36:17 CEST 2018
On Fri, 18 May 2018 21:18:03 +0700
Ryan Joseph <ryan at thealchemistguild.com> wrote:
> > On May 18, 2018, at 9:11 PM, Mattias Gaertner <nc-gaertnma at netcologne.de> wrote:
> >
> > var
> > o: TJSObject;
> > key: string;
> > begin
> > for key in o do writeln(o[key]);
> > end;
>
> I don’t understand this.
>
> 1) why are we talking about TJSObject? That’s an additional use for..in which is good to know but it’s not an array.
It works the same for all external classes. The For-in loop of a JS
object is translated to the JS for-in loop.
> 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?
Mattias
More information about the Pas2js
mailing list