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

Re: [jslint] The Comma Operator Causes JSLint to Stop Prematurely

Expand Messages
  • Marcel Duran
    Please see answer for question #4 from a popular JavaScript quiz: http://www.nczonline.net/blog/2010/02/23/answering-soshnikovs-quiz/ Marcel On Sat, Jul 17,
    Message 1 of 6 , Jul 18, 2010
    View Source
    • 0 Attachment
      Please see answer for question #4 from a popular JavaScript quiz:

      http://www.nczonline.net/blog/2010/02/23/answering-soshnikovs-quiz/

      Marcel

      On Sat, Jul 17, 2010 at 8:58 PM, Cheney, Edward A SSG RES USAR USARC <
      austin.cheney@...> wrote:

      >
      >
      > You are misusing the comma operator. Commas in JavaScript exist to separate
      > items in a list. Any other use of commas is invalid.
      >
      > Valid uses of commas:
      > var a, b, c = 1, d = 2, e;
      > var myArray = [a, b, c];
      >
      > Your example is invalid, because it is an attempt to assign two single
      > value data types to a single variable. Your example would be valid if you
      > replaced the parenthesis with square brackets as then you would be assigning
      > a list of values to an array. Your example would also be valid if you
      > removed all but one value/variable and all commas. Aside from those two
      > alternatives there is no other way to make your example valid.
      >
      > The logical implication of your example appears to be a tertiary operation,
      > which is assigning an "if" condition to a variable. Here is an example:
      >
      > var a, test = (a) ? 1:5;
      >
      > In that example the variable "a" is tested for an assignment. If "a" is
      > assigned a value "test" receives a value of 1 otherwise "test" receives a
      > value of 5.
      >
      > The JSLint error reporting might possibly be more complicated than
      > necessary considering the simplicity of your example, but it is not wrong.
      >
      > Austin Cheney
      > http://prettydiff.com/
      >
      >



      --
      Marcel Duran


      [Non-text portions of this message have been removed]
    • pauanyu
      I think you are missing the point: It would be nice if JSLint could recognize the Comma Operator and display a warning message because the Comma Operator is
      Message 2 of 6 , Jul 18, 2010
      View Source
      • 0 Attachment
        I think you are missing the point:

        "It would be nice if JSLint could recognize the Comma Operator and
        display a warning message because the Comma Operator is definately not
        one of Javascript's good parts. Unfortunately, GWT uses them a lot."

        What they are asking is that JSLint output a warning about using the comma operator, NOT that JSLint accept the comma operator as good code.

        In other words, rather than giving vague warnings that aren't specific to the problem, they want JSLint to specifically say "don't use the comma operator".

        --- In jslint_com@yahoogroups.com, "Cheney, Edward A SSG RES USAR USARC" <austin.cheney@...> wrote:
        >
        > You are misusing the comma operator. Commas in JavaScript exist to separate items in a list. Any other use of commas is invalid.
        >
        > Valid uses of commas:
        > var a, b, c = 1, d = 2, e;
        > var myArray = [a, b, c];
        >
        > Your example is invalid, because it is an attempt to assign two single value data types to a single variable. Your example would be valid if you replaced the parenthesis with square brackets as then you would be assigning a list of values to an array. Your example would also be valid if you removed all but one value/variable and all commas. Aside from those two alternatives there is no other way to make your example valid.
        >
        > The logical implication of your example appears to be a tertiary operation, which is assigning an "if" condition to a variable. Here is an example:
        >
        > var a, test = (a) ? 1:5;
        >
        > In that example the variable "a" is tested for an assignment. If "a" is assigned a value "test" receives a value of 1 otherwise "test" receives a value of 5.
        >
        > The JSLint error reporting might possibly be more complicated than necessary considering the simplicity of your example, but it is not wrong.
        >
        > Austin Cheney
        > http://prettydiff.com/
        >
      • urangatang@att.net
        Also, I noticed that JSLint does not throw any warning at all for the Comma Operator in the MDC example: for (var i=0, j=9; i
        Message 3 of 6 , Jul 20, 2010
        View Source
        • 0 Attachment
          Also, I noticed that JSLint does not throw any warning at all for the Comma Operator in the MDC example:

          for (var i=0, j=9; i <= 9; i++, j--)
          document.writeln("a["+i+"]["+j+"]= " + a[i][j]);

          Is that a mistake, or is this an acceptable use of commas?



          --- In jslint_com@yahoogroups.com, "urangatang@..." <urangatang@...> wrote:
          >
          > I'm trying to clean up some code with JSLint, but I have been running
          > into a problem with JSLint stopping prematurely when it comes across the
          > Comma Operator
          > <https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operator\s/Special_Operators/Comma_Operator> .
          > Here is a simple example:
        • Cheney, Edward A SSG RES USAR USARC
          That is perfectly valid and acceptable. for (var i=0, j=9; i
          Message 4 of 6 , Jul 21, 2010
          View Source
          • 0 Attachment
            That is perfectly valid and acceptable.

            for (var i=0, j=9; i <= 9; i++, j--)
            document.writeln("a["+i+"]["+j+"]= " + a[i][j]);

            It is a standard for loop with the required three semicolon separated sections. The first section is a single var command applied to a list of variables. The third section defines how an iteration of the loop is manifest. In this case the loop cause the "i" variable to increment by one and the "j" variable to simultaneously decrement by one.

            From a stylistic perspective this type of looping is risky if closures are contained in this loop. The loop is logically solid and straight forward because all that matter is the value of "i" for termination. I say this loop is risky because the loop changes the value of two variables simultaneously and independently. Any automated changes that occur simultaneously at absolute independence from each other always result in higher levels of complexity for debugging while possibly allowing a hidden conveyance for errors between separate pieces of consuming logic.

            Austin Cheney
            http://prettydiff.com/
          Your message has been successfully submitted and would be delivered to recipients shortly.