admin管理员组文章数量:1335361
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) +
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 plete statement.
Unfortunatly, when I mented 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.
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) +
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 plete statement.
Unfortunatly, when I mented 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.
- 5 Yep, think of the implication of what you're doing. – Demian Brecht Commented Sep 7, 2011 at 18:04
- 1 At the risk of being blunt, it sounds like you're ranting since you lost a lot of time on this minor error. Because of that, I think you're already less likely to make it in the future and don't need any special tricks to prevent this. – Peter Commented Sep 7, 2011 at 18:06
- You can read about automatic semi-colon insertion: es5.github./#x7.9 – James Allardice Commented Sep 7, 2011 at 18:06
- I do unserstand, why that happened, I just want to make sure, it doesn't happen again. – René Nyffenegger Commented Sep 7, 2011 at 18:10
- I'm not sure it helps with semi-colons, but javascript has a strict mode: developer.mozilla/en/JavaScript/Strict_mode Could help prevent other similar-typed mistakes, maybe? – mqchen Commented Sep 7, 2011 at 18:12
4 Answers
Reset to default 8Lazy 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".
版权声明:本文标题:Can I prevent stupid errors in JavaScript because the language does not require semicolons? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742249684a2440516.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论