简体   繁体   English

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

[英]Unexpected token operator «=», expected punc «,»

i am getting the following error 我收到以下错误

Parse error: Unexpected token operator «=», expected punc «,» Line 159, column 26 解析错误:意外的令牌运算符«=»,预期的punc«,»第159行,第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);
    }
  }

You are trying to use Default parameters , which are a bleeding edge feature of JavaScript with limited support . 您正在尝试使用默认参数 ,这是JavaScript的前沿功能, 支持有限

JS Lint rejects them unless you turn on the ES6 option. 除非你打开ES6选项,否则JS Lint会拒绝它们。

@Quentin is exactly right: You need the es6 option. @Quentin完全正确:你需要es6选项。

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). 还有更多的JSLint失败,但是,特别是你使用== ,这是一个“强制运算符” - 检查JSLint是否相等 - 以及jslint部分中bitwise选项 (没有直接指向jslint指令的链接,我不知道我想,所以我就在它上面联系了。 As @AxelH suggests, there's likely more you really want to ask us. 正如@AxelH所暗示的那样,你可能更想问我们。 ;^) ; ^)

Here's a version that lints on JSLint.com as it stands today. 下面是对绒毛版本JSLint.com因为它代表今天。 Note the /*jslint directive line at the top that includes the es6 tag : 注意顶部的/*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 is right with his answer. @Quentin的答案是正确的。 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. 我可以添加的是你可能会尝试删除EC6语法,并将你的函数重写为旧的JS。

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

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

A workaround to this issue could be this: 解决此问题的方法可能是:

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.

相关问题 错误:在最小化angularjs应用程序时出现了意外的标记punc«)»,预期的punc«,» - Error: Unexpected token punc «)», expected punc «,» while minifying angularjs app 意外的标记 punc «(»,从 UglifyJS 创建块时预期的 punc - Unexpected token punc «(», expected punc when creating chunk from UglifyJS SyntaxError:意外的标记:punc()) - SyntaxError: Unexpected token: punc ()) ExecJS::ProgramError: 运行 rake 资产时出现意外的标记 punc «(»,预期的 punc «:»:在生产中预编译 - ExecJS::ProgramError: Unexpected token punc «(», expected punc «:» when running rake assets:precompile on production Uglify SyntaxError:意外的标记:punc ()) - Uglify SyntaxError: Unexpected token: punc ()) UglifyJS的bundle.js中出现意外的令牌名称“ i”,预期的点是“;” - Unexpected token name «i», expected punc «;» Error in bundle.js from UglifyJS 意外标记:名称«Dom7»,预期:punc «;» 何时从 UglifyJs 创建块? - Unexpected token: name «Dom7», expected: punc «;» when create chunk from UglifyJs? 来自 UglifyJs 的 build.js 中的错误意外标记:punc (() - ERROR in build.js from UglifyJs Unexpected token: punc (() 消息:'意外的令牌:punc(。)',同时在grunt中使用uglify - message: 'Unexpected token: punc (.)', while using uglify in grunt SyntaxError: Unexpected token: operator (>) - SyntaxError: Unexpected token: operator (>)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM