我收到以下错误
解析错误:意外的令牌运算符«=»,预期的punc«,»Line 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);
}
}
答案 0 :(得分:22)
您正在尝试使用Default parameters,这是使用limited support的JavaScript的前沿功能。
除非你打开ES6选项,否则JS Lint会拒绝它们。答案 1 :(得分:2)
@Quentin完全正确:您需要es6
选项。
还有更多的JSLint失败,但是,特别是你使用==
,这是一个强制运营商" - 检查JSLint on equality - 和bitwise
option in the jslint
section(没有直接指向jslint指令的链接,我不这么认为,所以我只是在它上面链接)。正如@AxelH所暗示的那样,您可能更想问我们。 ; ^)
这是一个今天JSLint.com的版本。请注意顶部的/*jslint
指令行,其中包含es6
tag:
/*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);
}
}
答案 2 :(得分:1)
@Quentin的回答是正确的。由于他提到的原因,您会收到语法错误。我可以添加的是你可能会尝试删除EC6语法,并将你的函数重写为旧的JS。// change from function fitBounds(type="all", shape=null) // change to function fitBounds(type="all", shape)
答案 3 :(得分:0)
解决此问题的方法可能是:
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);
}
}