[Pas2js] Question regarding the New() function (#34736)
Michael Van Canneyt
michael at freepascal.org
Thu Dec 20 17:35:19 CET 2018
On Thu, 20 Dec 2018, silvioprog wrote:
> On Thu, Dec 20, 2018 at 1:09 PM Michael Van Canneyt <michael at freepascal.org>
> wrote:
>
>> On Thu, 20 Dec 2018, silvioprog wrote:
>>> ... a pascal-ish manner, should be:
>>>
>>> var
>>> o: TJSObject;
>>> begin
>>> o := TJSObject.new([
>>> 'foo', 'bar',
>>> 'object', TJSObject.new(['abc', 123]),
>>> 'array', TJSArray.new(1,2,3)
>>> ]);
>>> Writeln(TJSJSON.stringify(o));
>>> end.
>>
>> I agree. When we have type helpers, this will be possible.
>
>
> Great! :-)
>
> The parametrized constructor is already possible in TJSArray.new(), on each
> parameter is an array item, but it will be very useful something like this
> in TJSObject.new() too, on each item is a pair (key-value) and without the
> brackets. :-)
Yes.
The reason that we need to wait for type helpers is the following:
The feature you request requires code to be generated by the compiler, as
the standard Javascript object does not contain the calls you suggest.
But the external classes do not contain code.
In this sense they are comparable to interface definitions.
IMO it would not be good design to allow code in an external class definition.
So, the code cannot go in the external class definition, but it can go
in a type helper, which will then look something like this:
TObjectHelper = Class helper for TJSObject
constructor new(Values : TJSValueDynArray); varargs; overload;
constructor new(Name : String; Value : JSValue); varargs; overload;
// ++ Some other real killer functions you can think of...
end;
So, maybe you still want to change your vote in the poll ? ;-)
Michael.
More information about the Pas2js
mailing list