Which lines need semicolons for Dean Edwards Javascript packer?
-
28-04-2021 - |
Pregunta
So his help site states that "All statements, including function declarations, must be correctly terminated with semi-colons."
But in this sample code, he specifically say not to end the if statement in semicolon.
So is there a complete list of what should be ended with a semicolon? I was looking at my Javscript code and here are some cases I wasn't sure was proper format for the packer:
1)
for( i in cities ) {
alert( i );
};
2)
var map = {
city : 'atlanta',
year : 1987
};
3)
var info_window = new google.maps.InfoWindow( {
content : content_div,
zIndex : INFO_WINDOW_Z
}; );
4)
var options = {
business : business,
columns : [ 'url', 'image_url', 'expiration', 'percent_discount', 'claimed', 'fine_print' ];
};
5)
$( warp_content ).hover( function() {
$( deal_description ).fadeIn( 'fast' );
};, function() {
$( deal_description ).fadeOut( 'fast' );
}; );
Solución
There are rules, but sometimes they may not be obvious. Essentially, the following statements do not need terminating semicolons:
if (...) { }
for (...) { }
while (...) { }
(except indo { } while (...);
)function (...) { }
(except for examples such asvar f = function() { };
where an anonymous function is part of a larger statement)try { } catch (...) { }
with (...) { }
Essentially, anywhere { }
surrounds a group of statements, that is a block and no terminating semicolon is required.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow