What is the equivalent of this JS code in DWScript please? I use the DWScript codegen.
// JavaScript:
DoIt({name:"Fred", size:{width:3,height:2}});
I tried anonymous record but it seems not working:
var obj: variant;
obj := record
Name = 'Fred';
Size: variant = record
Width = 3;
Height = 2;
end;
end;
The generated JS code with DWScript Codegen is:
var obj = undefined,
/// anonymous TRecordSymbol
function Copy$a$460(s,d) {
return d;
}
function Clone$a$460($) {
return {
}
}
function Pub$a$460($) {
return {
"Name":$.Name$8
,"Size":$.Size$5
}
}
/// anonymous TRecordSymbol
function Copy$a$461(s,d) {
return d;
}
function Clone$a$461($) {
return {
}
}
function Pub$a$461($) {
return {
"Width":$.Width$4
,"Height":$.Height$2
}
}
obj = {Name$8:"Fred",Size$5:3};
alert(JSON.stringify(obj));
I will give you an example that will help you answer your own question
First, create a new type, something along the lines of:
type
TDimensions = record
published
Width: integer;
Height: integer;
end;
Next, you need to somehow use it. Imagine you click a button, a bunch of data is gathered from a few input fields, you sanitize your input and dispatch it. As a start use something like:
procedure TForm1.ButtonClickHandler(Sender: TObject);
var
dimensions: TDimensions;
payload: Variant;
serialized: String;
begin
dimensions.width := StrToInt(self.W3EditBox2.Text);
dimensions.height := StrToInt(self.W3EditBox3.Text);
payload := TVariant.CreateObject;
payload.name := self.W3EditBox1.Text;
payload.size := dimensions;
asm
@serialized = JSON.stringify(@payload);
end;
writeln(serialized);
end;