簡體   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