[Pas2js] for .. in loop

Michael Van Canneyt michael at freepascal.org
Fri May 18 17:06:56 CEST 2018



On Fri, 18 May 2018, Ryan Joseph 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.

Me neither. 
I don't understand why this compiles at all :)

>
> 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.
>
> 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.

No. The compiler has no way of knowing what the TJSArray contains.

So in the best case you should be able to do

var
   o: TJSArray;
   JS: JSVAlue;
begin
   for JS in o do writeln(JS);
end.


Michael.


More information about the Pas2js mailing list