I just lost quite a bit of time because I changed a JavaScript function that read (something like)
function F(a,b,c) {
return x(a,b,c) +
开发者_StackOverflow中文版 y(a,b,c) +
z(a,b,c);
}
to
function F(a,b,c) {
return // x(a,b,c) +
y(a,b,c) +
z(a,b,c);
}
when I needed to test something.
The changed function returns undefined
, of course, because the language does not require a semicolon and assumes the return
to be a complete statement.
Unfortunatly, when I commented out x(a,b,c)
I didn't think of this implication. So, is there a way to prevent such stupid misstakes in the future.
Lazy debugger's solution:
function F(a,b,c) {
return (
// x(a,b,c) +
y(a,b,c) +
z(a,b,c)
);
}
JSLint and a unit testing framework.
Problem at line 3 character 11: Expected ';' and instead saw 'y'.
Semicolon insertion is particularly nasty with return
. A workaround for this case: don't return
multiline statements.
function F(a,b,c)
{
var toReturn x(a,b,c) +
y(a,b,c) +
z(a,b,c);
return toReturn;
}
Integrate JSLint into your build, and fail builds when you detect
Problem at line 7 character 12: Unreachable 'y' after 'return'.
Make this generic for line and character, of course. What you're really looking for is "Unreachable after return".
精彩评论