[Pas2js] Question regarding the New() function (#34736)

Michael Van Canneyt michael at freepascal.org
Thu Dec 20 16:26:13 CET 2018



On Thu, 20 Dec 2018, silvioprog wrote:

> Hi.
>
> After #34736 was closed, unfortunately Mantis doesn't allow to comment, so
> I decided to ask here. (I don't like Mantis 😑)
>
> What do you think about this syntax sugar?
>
> uses JS;
>
> var
>  o: TJSObject;
> begin
>  o := newObject(
>    'foo', 'bar',
>    'object', newObject('abc', 123),
>    'array', newArray(1,2,3)
>  );
>  Writeln(TJSJSON.stringify(o));
> end.
>
> It is productive for me, instead of declaring a lot of local variable or or
> literal strings just to create a simple JSON which I could do in a single
> line, like:
>
> newObject('foo', 'bar','object', newObject('abc', 123),'array',
> newArray(1,2,3))
>
> So, how to use the New() function to develop the newObject() following the
> suggested syntax? For example, it doesn't compile:
>
> var
>  o: TJSObject;
> begin
>  o := new(
>    'foo', 'bar',
>    'object', new('abc', 123),
>    'array', TJSArray.new(1,2,3)
>  );

That should be

o := new([
     'foo', 'bar',
     'object', new(['abc', 123]),
     'array', TJSArray.new(1,2,3)
   ]);

Michael.


More information about the Pas2js mailing list