Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
    }
  }
like image 804
Harsha M V Avatar asked Nov 24 '16 07:11

Harsha M V


3 Answers

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.

like image 100
Quentin Avatar answered Oct 11 '22 16:10

Quentin


@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);
  }
}
like image 2
ruffin Avatar answered Oct 11 '22 17:10

ruffin


@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)
like image 1
Lukasz Dynowski Avatar answered Oct 11 '22 17:10

Lukasz Dynowski