i am getting the following error
Parse error: Unexpected token operator «=», expected punc «,» Line 159, column 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.
JS Lint rejects them unless you turn on the ES6 option.
@Quentin is exactly right: You need the es6
option.
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). As @AxelH suggests, there's likely more you really want to ask us. ;^)
Here's a version that lints on JSLint.com as it stands today. Note the /*jslint
directive line at the top that includes the 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);
}
}
@Quentin is right with his answer. 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.
// change from
function fitBounds(type="all", shape=null)
// change to
function fitBounds(type="all", shape)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With