意外的标记运算符 «=»,预期的 punc «,»

新手上路,请多包涵

我收到以下错误

解析错误:意外的标记运算符 «=»,预期的 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);
    }
  }

原文由 Harsha M V 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 357
2 个回答

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

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

原文由 Quentin 发布,翻译遵循 CC BY-SA 3.0 许可协议

@Quentin 完全正确:您需要 es6 选项。

然而,还有很多 JSLint 失败的地方,特别是您对 == 的使用,它是一个“强制运算符”——检查 JSLint 是否相等——以及 bitwise 中的选项 jslint 部分(没有直接指向 jslint 指令的链接,我不认为,所以我在它上面链接)。正如@AxelH 所建议的,您可能真的想问我们更多问题。 ;^)

这是今天在 JSLint.com 上进行 lints 的版本。请注意顶部的 /*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);
  }
}

原文由 ruffin 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题