[Pas2js] CreateObject

Michael Van Canneyt michael at freepascal.org
Wed Aug 14 23:42:51 CEST 2019



On Wed, 14 Aug 2019, warleyalex via Pas2js wrote:

> Hey, I'd like to create an instance of this class/interface definition:
> -----------------------------------------------------------------------------
> TGoogleMapOptions = class external name 'google.maps.MapOptions'
>  private
>    fcenter: TGoogleLatLng; external name 'center';
>    fmapTypeId: String; external name 'mapTypeId';
>    fzoom: Integer; external name 'zoom';
>  public
>     property center: TGoogleLatLng read fcenter write fcenter;
>     property mapTypeId: String read fmapTypeId write fmapTypeId;
>     property zoom: Integer read fzoom write fzoom;
>  end; 
> -----------------------------------------------------------------------------
>
> You can't create an instance of this interface, just because it's not
> available through. Alright.
> Yes, there's an workaround:
> ==============================
> var
>  CreateObject: TJSObject; external name '{}';
>
> var
>  mapOptions: TGoogleMapOptions; 
>
> begin
>  mapOptions := TGoogleMapOptions(CreateObject);
>  mapOptions.zoom:= 16;
>  mapOptions.center := TGoogleLatLng.New(-19.4574, -44.2417);
>  mapOptions.mapTypeId := TGoogleMapTypeID.ROADMAP;
> 
> pas2js will generate this:
>
> 	var mapOptions = null;
> 	mapOptions = {};
> 	mapOptions.zoom = 16;
> 	mapOptions.center = new google.maps.LatLng(-19.4574,-44.2417,false);
> 	mapOptions.mapTypeId = google.maps.MapTypeId.ROADMAP;
>
> I think the external CreateObject variable should live in the JS unit!

You can use the new() function for this.

  mapOptions := TGoogleMapOptions(New([]));

Michael.


More information about the Pas2js mailing list