[英]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.