Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / pas2js / 2019-08

CreateObject

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

warleyalex
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!

Reply by email

Michael Van Canneyt
to warleyalex
warleyalex wrote 39 lines
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.

Reply by email