最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Can I prevent stupid errors in JavaScript because the language does not require semicolons? - Stack Overflow

programmeradmin5浏览0评论

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.

Share Improve this question asked Sep 7, 2011 at 18:03 René NyffeneggerRené Nyffenegger 40.6k34 gold badges178 silver badges315 bronze badges 5
  • 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
Add a ment  | 

4 Answers 4

Reset to default 8

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".

发布评论

评论列表(0)

  1. 暂无评论