[Pas2js] TJSXMLHttpRequest questions

Michael Van Canneyt michael at freepascal.org
Sun May 20 14:56:00 CEST 2018



On Sun, 20 May 2018, Ryan Joseph wrote:

>
>
>> On May 20, 2018, at 5:51 PM, Mattias Gaertner <nc-gaertnma at netcologne.de> wrote:
>> 
>> There are no anonymous methods yet, but there are closures:
>> 
>> procedure Test;
>> var
>>  xhr : TJSXMLHttpRequest;
>>
>>  procedure HandleLoaded;
>>  begin
>>    if (xhr.readyState = 4) or (xhr.status = 200) then
>>      writeln(xhr.responseText);
>>  end;
>> 
>> begin
>>  xhr := TJSXMLHttpRequest.new;
>>  xhr.open('GET', 'file.html');
>>  xhr.onreadystatechange := @HandleLoaded;
>>  xhr.send;
>> end;
>
> I thought closures were what FPC was calling anonymous methods. That’s a pretty important feature for JavaScript.

You can think of local methods as closures with a name...

>
> That means we need to have a global out in the stack for every time we need to use TJSXMLHttpRequest? I guess we could rely on a “controller” class or something to make this manageable (is that even possible to use a class method?). Could subclassing TJSXMLHttpRequest be an option?

Till now I used encapsulation. It's easier.

The onreadystatechange is somewhat problematic in the sense that is has no
parameters; It's easier to use the onload event.

If you install an event handler for readystatechange using addeventlistener, you should normally
get an event object passed, with the target set to the TJSXMLHttpRequest
instance.

Michael.


More information about the Pas2js mailing list