繁体   English   中英

意外的令牌运算符«=»,预期的punc«,»

[英]Unexpected token operator «=», expected 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);
    }
  }

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

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

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

还有更多的JSLint失败,但是,特别是你使用== ,这是一个“强制运算符” - 检查JSLint是否相等 - 以及jslint部分中bitwise选项 (没有直接指向jslint指令的链接,我不知道我想,所以我就在它上面联系了。 正如@AxelH所暗示的那样,你可能更想问我们。 ; ^)

下面是对绒毛版本JSLint.com因为它代表今天。 注意顶部的/*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);
  }
}

@Quentin的答案是正确的。 由于他提到的原因,您会收到语法错误。 我可以添加的是你可能会尝试删除EC6语法,并将你的函数重写为旧的JS。

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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