[Pas2js] Bootstrap & Helper techniques

warleyalex warleyalex at yahoo.com.br
Sun Sep 29 16:02:52 CEST 2019


OK. In short, we can achieve (pass on object argument) the same effect with:

var
  o: TTOptions;
  op: TTableOptions;
  rec: TShopItem;

begin
  { Test I - it is possible to pass a parameter "inline" without having to
declare a variable of that type first, 
     but we don't have the code completion
  }
  DoSomething1( New(['MyName','Test1','Price',1, 'Func', @Go]) );

  { Test II - inherited from TJSObject to access the constuctor }
  o := TTOptions.new;
  o.MyName:= 'Test2';
  o.Price:= 2;
  o.Func:= @Go;
  DoSomething2(o);

  { Test III - it is not necessary to inherite from TJSObject at all }
  op := TTableOptions(CreateObject);
  op.MyName:= 'Test3';
  op.Price:= 3;
  op.Func:= @Go;
  DoSomething3(op);

  { Test IV - using a factory function together with the record }
  DoSomething4(Options('Test4',4, @Go));

  { Test V - using record type }
  rec.MyName:= 'Test5';
  rec.Price:= 5;
  rec.Func:= @Go;
  DoSomething5(rec);

  { Test VI - using advanced record approach }
  DoSomething6( TMyShopItem.Create('Test6', 6, @Go) );

  { Test VII - using static array of record }
  //console.log(staticObjects[0].MyName );
 // console.log(staticObjects[0].Price );
  DoSomething7(staticObjects[0]);

//-
CODE------------------------------------------------------------------------
type
  TShopItem = record
  MyName : string;
  Price : Double;
  Func: TProcedure;
end;

type
  { TMyShopItem }
  TMyShopItem = record
  MyName : string;
  Price : Double;
  Func: TProcedure;
  constructor Create(Name: String; Price: Double; Func: Tprocedure);
end;

type
  TTOptions = class external name 'Object' (TJSObject)
    MyName : string;
    Price : Double;
    Func: TProcedure;
  end;

type
  TTableOptions = class external name 'TTableOptions'
    MyName : string;
    Price : Double;
    Func: TProcedure;
  end;

  var
    CreateObject : TJSObject external name '{}';

implementation

{ TMyShopItem }
constructor TMyShopItem.Create(Name: String; Price: Double; Func:
Tprocedure);
begin
  Self.MyName := Name;
  Self.Price  := Price;
  Self.Func   := Func;
end;


//---------
procedure Go;
begin
  console.log('GO!');
end;

var
  staticObjects: array of TShopItem =
    (
      (MyName : 'Test7';  Price : 7 {; Func: @Go} ),
      (MyName : 'Pencil'; Price : 15.75),
      (MyName : 'Board';  Price : 42.96)
    );

type
  TName = record
    MyName: String;
    Price: Double;
    Func: TProcedure;
  end;

  function Options(Name: String; Price: double; Func: TProcedure ): TName;
  begin
    Result.MyName := Name;
    Result.Price := Price;
    Result.Func := Func;
  end;

  procedure DoSomething1(options: TJSObject);
  begin
    console.log(options);
  end;

  procedure DoSomething2(options: TTOptions);
  begin
    console.log(options);
  end;

  procedure DoSomething3(options: TTableOptions);
  begin
    console.log(options);
  end;

  procedure DoSomething4(theRec : TName);
  begin
    console.log(theRec);
  end;

  procedure DoSomething5(theRec : TShopItem);
  begin
    console.log(theRec);
  end;

  procedure DoSomething6(theRec : TMyShopItem);
  begin
    console.log(theRec);
  end;

  procedure DoSomething7(theRec : TShopItem);
  begin
    console.log(theRec);
  end;





--
Sent from: http://pas2js.38893.n8.nabble.com/


More information about the Pas2js mailing list