javascript - 3.58 Breaking Change? - Stack Overflow

My mapping application broke with the upgrade to Google Maps JavaScript API v3.58 and above. I am tryin

My mapping application broke with the upgrade to Google Maps JavaScript API v3.58 and above. I am trying to create a new google.maps.Polyline using a custom class that extends the Polyline with a few new methods I use frequently.

The error in the console is as follows:

TypeError: Class constructors cannot be invoked without 'new'
at new CustomPolyline.Polyline (webpack-internal:///...tomPolygon.js:31:32)

I create instances of my custom class using

  polylines.push(new CUST.CustomPolyline.Polyline(polygonType, this.mapInstance, polylineJSONObjects[i].coordinates, polylineJSONObjects[i].normalPolyAttributes, polylineJSONObjects[i].highlightPolyAttributes, polylineJSONObjects[i].normalPolyAttributes.mapLabelText));

The code executing is the constructor. I'm using a namespace called CUST and my extension is called CustomPolyline

'use strict';
(
  function (window, CUST, google) {

  if (typeof (CUST) === 'undefined') {
    CUST = {};
  }

  CUST.CustomPolyline.Polyline = function(polyType, googleMap, dvgCoordinatesString, 
                                       normalPolyAttributes, highlightPolyAttributes,
                                       labelText) {
  // Constructor
  if (this instanceof CustomPolyline.Polyline) {
    try {
      google.maps.Polyline.apply(this, arguments);
    } catch (ex) {
      // new Chrome update gave exception in 'apply' so used 'call'
      google.maps.Polyline.call(this, arguments);
    }

    this.base = google.maps.Polyline.prototype;

    <... constructor initialization code ...>
  }
 
  <... extension methods ...>

)(window, window.CUST, window.google);

If I switch to v3.57, this code works fine. I get the above error with any version newer than 3.57. I have reviewed all the release notes and see no indication of a breaking change in 3.58 that would cause this.

Could this be an ECMAScript version problem with the Google Maps API? If so, how can I fix it without rewriting my extension class completely (it's thousands of lines of code)?

My mapping application broke with the upgrade to Google Maps JavaScript API v3.58 and above. I am trying to create a new google.maps.Polyline using a custom class that extends the Polyline with a few new methods I use frequently.

The error in the console is as follows:

TypeError: Class constructors cannot be invoked without 'new'
at new CustomPolyline.Polyline (webpack-internal:///...tomPolygon.js:31:32)

I create instances of my custom class using

  polylines.push(new CUST.CustomPolyline.Polyline(polygonType, this.mapInstance, polylineJSONObjects[i].coordinates, polylineJSONObjects[i].normalPolyAttributes, polylineJSONObjects[i].highlightPolyAttributes, polylineJSONObjects[i].normalPolyAttributes.mapLabelText));

The code executing is the constructor. I'm using a namespace called CUST and my extension is called CustomPolyline

'use strict';
(
  function (window, CUST, google) {

  if (typeof (CUST) === 'undefined') {
    CUST = {};
  }

  CUST.CustomPolyline.Polyline = function(polyType, googleMap, dvgCoordinatesString, 
                                       normalPolyAttributes, highlightPolyAttributes,
                                       labelText) {
  // Constructor
  if (this instanceof CustomPolyline.Polyline) {
    try {
      google.maps.Polyline.apply(this, arguments);
    } catch (ex) {
      // new Chrome update gave exception in 'apply' so used 'call'
      google.maps.Polyline.call(this, arguments);
    }

    this.base = google.maps.Polyline.prototype;

    <... constructor initialization code ...>
  }
 
  <... extension methods ...>

)(window, window.CUST, window.google);

If I switch to v3.57, this code works fine. I get the above error with any version newer than 3.57. I have reviewed all the release notes and see no indication of a breaking change in 3.58 that would cause this.

Could this be an ECMAScript version problem with the Google Maps API? If so, how can I fix it without rewriting my extension class completely (it's thousands of lines of code)?

Share Improve this question edited Mar 3 at 15:34 jonrsharpe 122k30 gold badges268 silver badges476 bronze badges asked Mar 3 at 15:28 Terence WightTerence Wight 6711 bronze badges 2
  • CUST.CustomPolyline.Polyline = function... - you checked that CUST is defined and create an empty object if not,, but in that case CUST.CustomPolyline is undefined. – James Commented Mar 3 at 16:34
  • @James This is a closure. It can only be defined once, so the code needs to check for it any time it runs. – Terence Wight Commented Mar 4 at 20:51
Add a comment  | 

1 Answer 1

Reset to default 2

Your code uses "function constructors" from the pre-ES6 world, and so did the Google Maps Javascript API until version 3.57. Search for

_.to=function(a){ro.call(this,a)};

in https://maps.googleapis/maps-api-v3/api/js/57/13/main.js, this is what eventually becomes google.maps.Polyline.

But as of version 3.58 the Google Maps Javascript API uses ES6 classes, the code corresponding to google.maps.Polyline in https://maps.googleapis/maps-api-v3/api/js/58/11a/main.js is

_.tq=class extends Ln{

See here for a comparison of the syntax. Importantly, ES6 constructors cannot be invoked without the new keyword. Good question whether the switch to ES6 classes deserves to be called a breaking change and mentioned in the release notes.

You can change your code so that CUST.CustomPolyline.Polyline is a subclass in the ES6 sense:

CUST.CustomPolyline.Polyline = class extends google.maps.Polyline {
  constructor(polyType, googleMap, dvgCoordinatesString, 
              normalPolyAttributes, highlightPolyAttributes,
              labelText) {
    super(polyType, googleMap, dvgCoordinatesString, 
          normalPolyAttributes, highlightPolyAttributes,
          labelText);
    this.base = google.maps.Polyline.prototype;
    <... constructor initialization code ...>
  }
};
<... extension methods ...>

This assumes that you define your extension methods like

CUST.CustomPolyline.Polyline.prototype.method = function(...) {...}

But it would be more in line with the new ES6 class syntax to define them inside the class { ... }.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745086526a4610437.html

相关推荐

  • javascript - 3.58 Breaking Change? - Stack Overflow

    My mapping application broke with the upgrade to Google Maps JavaScript API v3.58 and above. I am tryin

    22小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信