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

Re: [jslint] Re: option.type

Expand Messages
  • Joshua Bell
    ... If value might be undefined or null you re better off using String(value). ... On the thread in general... Running some of my code through JSLint I m
    Message 1 of 9 , Jun 6 4:00 PM
    • 0 Attachment
      On Mon, Jun 6, 2011 at 3:08 PM, Erik Eckhardt <erik@...> wrote:

      > I think it's value.toString() not String(value). Correct me if I'm wrong.


      If value might be undefined or null you're better off using String(value).

      ...

      On the thread in general...

      Running some of my code through JSLint I'm (thankfully) seeing only a few
      occurrences of the "Type inconsistency" error:

      * One case where an optional argument is being tested against undefined then
      used in a string context:

      function f(a) {
      if (a !== (void 0)) {
      return "foo: " + a;
      }
      else {
      return "foo";
      }
      }

      Note that if the test is against the identifier "undefined" then the error
      goes away, it's only the "(void 0)" construction that triggers it. (I'm
      weird and don't trust the window.undefined property which is writable in
      some browsers.) This indicates that there's likely a special case for
      testing against undefined already. Arguably (pun intended) my code could
      test against arguments.length instead.

      * Construction of exception messages where a number is being concatenated
      into a string. In this case, wrapping with String() resolves the warning and
      probably improves maintainability of the code.

      * Cases where the variable is being used to hold different types, e.g. a
      string or number, and not in a type-agnostic context:

      var default_value = (var_type === 'string') ? '' : 0;

      These are infrequent enough that I can wrap them in a function and mark it
      with /*jslint type:true*/ to indicate the intent.


      [Non-text portions of this message have been removed]
    Your message has been successfully submitted and would be delivered to recipients shortly.