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

(expression).method() leads to parser error

Expand Messages
  • crlender
    This line works: var y = (arr[2] || ).toLowerCase(); This one doesn t (see output below): (arr[2] || ).toLowerCase(); By itself, that expression isn t very
    Message 1 of 3 , Nov 4, 2008
    View Source
    • 0 Attachment
      This line works:

      var y = (arr[2] || "").toLowerCase();

      This one doesn't (see output below):

      (arr[2] || "").toLowerCase();

      By itself, that expression isn't very useful when the result isn't
      used, but the error also occurs when the forEach method for arrays
      is applied later on:

      (arr[2] || "").split("|").forEach(function(x) {
      // do something with x
      });

      To make JSLint accept that construct, I would have to introduce a
      dummy variable, either like this:

      var dummy = (arr[2] || "").split("|").forEach(function(x) {
      // do something with x
      });

      or like this:

      var dummy = arr[2] || "";
      dummy.split("|").forEach(function(x) {
      // do something with x
      });

      This isn't just restricted to forEach; every statement of the form
      "(expression).method()" leads to the same error. Another (shorter)
      example would be:

      (obj || makeObj()).display();


      - Conrad


      _______________________________
      JSLint output:

      Lint at line 1 character 15: Missing semicolon.
      (arr[2] || "").toLowerCase();

      Lint at line 1 character 15: Expected an identifier and instead saw '.'.
      (arr[2] || "").toLowerCase();

      Lint at line 1 character 15: Stopping, unable to continue. (0% scanned).
    • Douglas Crockford
      ... JSLint now accepts statements like that. JSLint now treats let as a reserved word in anticipation of future standards.
      Message 2 of 3 , Nov 7, 2008
      View Source
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "crlender" <crlender@...> wrote:
        > This one doesn't (see output below):
        >
        > (arr[2] || "").toLowerCase();

        JSLint now accepts statements like that.

        JSLint now treats let as a reserved word in anticipation of future
        standards.
      • crlender
        ... Wow, that was fast. Tested and confirmed. Thanks! - Conrad
        Message 3 of 3 , Nov 8, 2008
        View Source
        • 0 Attachment
          --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...>
          wrote:
          > > (arr[2] || "").toLowerCase();
          >
          > JSLint now accepts statements like that.

          Wow, that was fast.
          Tested and confirmed.

          Thanks!


          - Conrad
        Your message has been successfully submitted and would be delivered to recipients shortly.