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

303(expression).method() leads to parser error

Expand Messages
  • crlender
    Nov 4, 2008
      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).
    • Show all 3 messages in this topic