Loading ...
Sorry, an error occurred while loading the content.

JSLint doesn't catch function declarations inside conditional blocks

Expand Messages
  • paulcrowder1979
    This code fails because strict mode prohibits function declarations in conditional blocks like switch and if statements, but JSLint doesn t catch this.
    Message 1 of 2 , Aug 16, 2011
    View Source
    • 0 Attachment
      This code fails because strict mode prohibits function declarations in conditional blocks like "switch" and "if" statements, but JSLint doesn't catch this. This is particularly difficult to track down since neither Chrome nor Firefox shows an error; it just fails to parse the code and continues as if it weren't even there.

      (function () {
      "use strict";

      var i = 1;

      switch (i) {
      case 1:
      function thisFailsInStrictMode() {
      // Function declarations can't appear in a conditional statement.
      }

      thisFailsInStrictMode();

      break;
      }

      alert("This code won't be reached.");
      }());
    • Douglas Crockford
      ... Thanks. Please try it now.
      Message 2 of 2 , Aug 16, 2011
      View Source
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "paulcrowder1979" <paul@...> wrote:
        >
        > This code fails because strict mode prohibits function declarations in conditional blocks like "switch" and "if" statements, but JSLint doesn't catch this. This is particularly difficult to track down since neither Chrome nor Firefox shows an error; it just fails to parse the code and continues as if it weren't even there.
        >
        > (function () {
        > "use strict";
        >
        > var i = 1;
        >
        > switch (i) {
        > case 1:
        > function thisFailsInStrictMode() {
        > // Function declarations can't appear in a conditional statement.
        > }
        >
        > thisFailsInStrictMode();
        >
        > break;
        > }
        >
        > alert("This code won't be reached.");
        > }());
        >

        Thanks. Please try it now.
      Your message has been successfully submitted and would be delivered to recipients shortly.