[Pas2js] for .. in loop
Ryan Joseph
ryan at thealchemistguild.com
Fri May 18 17:14:07 CEST 2018
> On May 18, 2018, at 10:06 PM, Michael Van Canneyt <michael at freepascal.org> wrote:
>
>
> Me neither. I don't understand why this compiles at all :)
I didn’t know that feature exist. Those are called object literals I think and are like hash tables in JS as far as I know.
>
>>
>> 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.
That’s what I wanted to do. It makes sense to use JSValue as the iterator and cast OR you should be able to cast the iterator like:
var
o: TJSArray;
i: integer;
begin
for integer(i) in o do writeln(JS);
end.
I guess the compiler could infer the cast from the iterator type though. Either way we need to cast back the JSValue to integer so it can be used.
Regards,
Ryan Joseph
More information about the Pas2js
mailing list