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

1010Re: option.undef

Expand Messages
  • Aseem
    Dec 2, 2009
    • 0 Attachment
      I think the intention is right, but to be honest, I've turned it off on a few occasions as a workaround to allow code to be in a different order.

      By default, JSLint requires variables to be declared before they're used. That makes sense in general, but there are times where helper functions are better off lower in the file, physically.

      function SomeClass() {
      // ...
      helperFunction1();
      helperFunction2();
      }

      function helperFunction1() {
      // ...
      }

      function helperFunction2() {
      // ...
      }

      To me, that code is just fine and readable. But it fails JSLint with undef set to true.

      In this case, we could simply order the functions differently. If the helper functions are purely stand-alone, sure. But in many cases, they depend on the semantics of SomeClass, e.g. its properties. So it makes more sense to put them below the SomeClass logic.

      Another way of writing it, aside from ordering it differently, to not fail would be something like this.

      var helperFunction1, helperFunction2;

      function SomeClass() {
      // ...
      helperFunction1();
      helperFunction2();
      }

      helperFunction1 = function () {
      // ...
      }

      helperFunction2 = function () {
      // ...
      }

      But I haven't seen any added benefit to that style. If you're nitpicking, that in fact increases code size. But more importantly, I don't think it increases readability or anything like that.

      So as a temporary workaround, I've been setting undef to false after verifying that I'm not referencing any truly undefined variables.

      Aseem

      --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...> wrote:
      >
      > I am thinking to remove the Disallow undefined variables. I added the option originally to make the transition easier for people with sloppy code. But that was years ago, and it is clear that disallowing undefined variables is simply the right thing. Would anyone be bothered by losing the ability to turn it off?
      >
    • Show all 9 messages in this topic