简体   繁体   中英

Unexpected token operator «=», expected punc «,»

i am getting the following error

Parse error: Unexpected token operator «=», expected punc «,» Line 159, column 26

This is my code

  function fitBounds(type="all", shape=null) {
    var bounds = new google.maps.LatLngBounds();

    if ( type == "all" ){
      if ((circles.length > 0) | (polygons.length > 0)){
        $.each(circles, function(index, circle){
          bounds.union(circle.getBounds());
        });
        $.each(polygons, function(index, polygon){
          polygon.getPath().getArray().forEach(function(latLng){
            bounds.extend(latLng);
          });
        });
      }
    }
    else if ( (type == "single") && (shape != null) ) {
      if (shape.type == google.maps.drawing.OverlayType.MARKER) {
        marker_index = markers.indexOf(shape);
        bounds.union(circles[marker_index].getBounds());
      }
      else {
        shape.getPath().getArray().forEach(function(latLng){
          bounds.extend(latLng);
        });
      }
    }

    if (bounds.isEmpty() != true)
    {
      map.fitBounds(bounds);
    }
  }

You are trying to use Default parameters , which are a bleeding edge feature of JavaScript with limited support .

JS Lint rejects them unless you turn on the ES6 option.

@Quentin is exactly right: You need the es6 option.

There's lots more that fails JSLint, however, particularly your use of == , which is a "coercing operator" -- check JSLint on equality -- and the bitwise option in the jslint section (there's no link directly to jslint directives, I don't think, so I linked just above it). As @AxelH suggests, there's likely more you really want to ask us. ;^)

Here's a version that lints on JSLint.com as it stands today. Note the /*jslint directive line at the top that includes the es6 tag :

/*jslint es6, white, browser */
/*global google, $ */

// These weren't declared, so I'm assuming they're
// within scope in your snippet's context. 
// I put others that felt like globals (google, $) 
// into globals, above.
var marker_index; 
var markers;
var circles;
var polygons;
var map;

function fitBounds(type="all", shape=null) {
  var bounds = new google.maps.LatLngBounds();

  if ( type === "all" ){
    // not sure why you're using bitwise `|` here. 
    // I think this'll be equivalent, though you should
    // be able to set `bitwise` as an option if it's not.
    // Still, you're evaluating to booleans, so `|` doesn't 
    // seem appropriate here.
    if ((circles.length > 0) || (polygons.length > 0)){
      $.each(circles, function(ignore, circle){
        bounds.union(circle.getBounds());
      });
      $.each(polygons, function(ignore, polygon){
        polygon.getPath().getArray().forEach(function(latLng){
          bounds.extend(latLng);
        });
      });
    }
  }
  else if ( (type === "single") && (shape !== null) ) {
    if (shape.type === google.maps.drawing.OverlayType.MARKER) {
      marker_index = markers.indexOf(shape);
      bounds.union(circles[marker_index].getBounds());
    }
    else {
      shape.getPath().getArray().forEach(function(latLng){
        bounds.extend(latLng);
      });
    }
  }

  if (!bounds.isEmpty())
  {
    map.fitBounds(bounds);
  }
}

@Quentin is right with his answer. You get syntax error due to reasons that he mentioned. What I can add to it is that you might try to drop EC6 syntax, and rewrite your function to old good JS.

// change from
function fitBounds(type="all", shape=null)

// change to
function fitBounds(type="all", shape)

A workaround to this issue could be this:

function fitBounds(type, shape_aux) {
    var bounds = new google.maps.LatLngBounds();
    if(typeof type === "undefined") {
      type = "all";
    }
    if(typeof shape_aux !== undefined) {
     shape = shape_aux;
    } else {
     shape = null;
    }
    if ( type == "all" ){
      if ((circles.length > 0) | (polygons.length > 0)){
        $.each(circles, function(index, circle){
          bounds.union(circle.getBounds());
        });
        $.each(polygons, function(index, polygon){
          polygon.getPath().getArray().forEach(function(latLng){
            bounds.extend(latLng);
          });
        });
      }
    }
    else if ( (type == "single") && (shape != null) ) {
      if (shape.type == google.maps.drawing.OverlayType.MARKER) {
        marker_index = markers.indexOf(shape);
        bounds.union(circles[marker_index].getBounds());
      }
      else {
        shape.getPath().getArray().forEach(function(latLng){
          bounds.extend(latLng);
        });
      }
    }

    if (bounds.isEmpty() != true)
    {
      map.fitBounds(bounds);
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM