意外的标记运算符 «=»,预期的双关语 «,»

问题描述 投票:0回答:4

我收到以下错误

解析错误:意外的标记运算符 «=»,预期的 punc «,» 行 159,第 26 栏

这是我的代码

  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);
    }
  }
javascript jslint
4个回答
31
投票

您正在尝试使用默认参数,这是 JavaScript 的一项前沿功能,且支持有限

JS Lint 会拒绝它们,除非您打开 ES6 选项。


2
投票

@Quentin 完全正确:您需要

es6
选项。

然而,还有很多 JSLint 失败的地方,特别是您使用

==
,这是一个“强制运算符”——检查 JSLint 的相等性——以及 bitwise
 部分中的 
jslint
选项
(我认为没有直接指向 jslint 指令的链接,所以我在它上面链接了)。正如 @AxelH 所建议的,您可能确实想问我们更多问题。 ;^)

这是目前在 JSLint.com 上 lint 的版本。请注意顶部的

/*jslint
指令行,其中包含
es6
标签
:

/*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);
  }
}

1
投票

@Quentin 他的回答是正确的。由于他提到的原因,您会遇到语法错误。我可以补充的是,你可以尝试放弃 EC6 语法,并将你的函数重写为旧的好的 JS。

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

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

1
投票

此问题的解决方法可能是这样的:

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);
    }
  }
© www.soinside.com 2019 - 2024. All rights reserved.