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

[jslint] Re: expected a conditional statement and instead saw an assignment

Expand Messages
  • Philip Hutchison
    ... I see... so wrapping the assignment in braces is simply a way to signify that yes, you intended to do an assignment. Slightly more verbose, but in the end
    Message 1 of 10 , Jan 10, 2009
    • 0 Attachment
      --- In jslint_com@yahoogroups.com, "Andy Stevens"
      <insomniacpenguin@...> wrote:
      >
      > 2009/1/10 Philip Hutchison <platelunch@...>:
      > > Sorry to dig up an old thread, but I don't quite understand Douglas'
      > > response, and this is a topic that's I've been curious about ever
      > > since I started using JSLint.
      > >
      > > montago's link gave this example:
      > >
      > > var rows = document.getElementsByTagName('tr');
      > > for( var i = 0, row; row = rows[i]; i++ ) {
      > > row.className = 'newclass';
      > > row.style.color = 'red';
      > > ...
      > > }
      > >
      > > I've used this approach many times; it's quite handy and has worked
      > > well for me except when I run it through JSLint.
      > >
      > > Does this mean the following statement -- which is functionally an
      > > equivalent of the previous example -- is also bad form? If so, why? I
      > > was under the impression it's the standard way to go.
      > >
      > > var rows = document.getElementsByTagName('tr');
      > > for( var i = 0; i < rows.length; i++ ) {
      > > var row = rows[i];
      > > row.className = 'newclass';
      > > row.style.color = 'red';
      > > ...
      > > }
      >
      > "var true;" or "var (1==1);" wouldn't be valid, so there's no
      > ambiguity about "var row=rows[i]" - it's obviously an assignment.
      > The problem with for(var i = 0, row; row = rows[i]; i++) is that
      > for(var i = 0, row; row == rows[i]; i++)
      > would also be valid syntax (but not necessarily resulting in the same
      > behaviour). Because using a single equals when you mean a double one
      > is a common mistake (one which you may or may not get away with),
      > jslint highlights the ambiguity. If, as in this case, the assignment
      > is actually intended, you can prevent jslint from displaying a warning
      > by surrounding it in braces.
      >
      > Does that help any?
      >
      >
      > Andy.
      > --
      > http://pseudoq.sourceforge.net/ Open source java sudoku solver
      >
      > > Not being a JavaScript guru, I don't understand what makes the second
      > > example an improvement over the first (except perhaps for legibility),
      > > and I don't understand how Douglas'examples are better than the
      > > original example. I would appreciate any clarification anyone can
      > > provide.
      > >
      > > Thanks!
      > > - philip
      >


      I see... so wrapping the assignment in braces is simply a way to
      signify that yes, you intended to do an assignment. Slightly more
      verbose, but in the end it becomes clearer and more readable code.

      This means the following statement passes JSLint and still does what
      I'd like it to (performing an assignment in the 'for' statement):

      var rows = document.getElementsByTagName('tr');
      for(var i=0, row; (row = rows[i]); i++){
      row.className = 'newclass';
      row.style.color = 'red';
      }

      Thanks for the clarification.
    • Daniel Cassidy
      2009/1/10 Andy Stevens ... As well as that, the two loops are *not* equivalent. The first loops until it reaches *either* the
      Message 2 of 10 , Jan 15, 2009
      • 0 Attachment
        2009/1/10 Andy Stevens <insomniacpenguin@...>
        >
        > 2009/1/10 Philip Hutchison <platelunch@...>:
        >
        > > Sorry to dig up an old thread, but I don't quite understand Douglas'
        > > response, and this is a topic that's I've been curious about ever
        > > since I started using JSLint.
        > >
        > > montago's link gave this example:
        > >
        > > var rows = document.getElementsByTagName('tr');
        > > for( var i = 0, row; row = rows[i]; i++ ) {
        > > row.className = 'newclass';
        > > row.style.color = 'red';
        > > ...
        > > }
        > >
        > > I've used this approach many times; it's quite handy and has worked
        > > well for me except when I run it through JSLint.
        > >
        > > Does this mean the following statement -- which is functionally an
        > > equivalent of the previous example -- is also bad form? If so, why? I
        > > was under the impression it's the standard way to go.
        > >
        > > var rows = document.getElementsByTagName('tr');
        > > for( var i = 0; i < rows.length; i++ ) {
        > > var row = rows[i];
        > > row.className = 'newclass';
        > > row.style.color = 'red';
        > > ...
        > > }
        >
        > "var true;" or "var (1==1);" wouldn't be valid, so there's no
        > ambiguity about "var row=rows[i]" - it's obviously an assignment.
        > The problem with for(var i = 0, row; row = rows[i]; i++) is that
        > for(var i = 0, row; row == rows[i]; i++)
        > would also be valid syntax (but not necessarily resulting in the same
        > behaviour). Because using a single equals when you mean a double one
        > is a common mistake (one which you may or may not get away with),
        > jslint highlights the ambiguity. If, as in this case, the assignment
        > is actually intended, you can prevent jslint from displaying a warning
        > by surrounding it in braces.
        >
        > Does that help any?

        As well as that, the two loops are *not* equivalent. The first loops
        until it reaches *either* the end of the array or a falsy value (e.g.
        false, zero, undefined, etc). The second always loops until the end of
        the array.

        Dan.
      • Daniel Cassidy
        ... (granted in this specific case the DOM API guarantees the array will not contain any falsy values, but the same is not true for many other arrays).
        Message 3 of 10 , Jan 15, 2009
        • 0 Attachment
          2009/1/15 Daniel Cassidy <mail@...>:
          > As well as that, the two loops are *not* equivalent. The first loops
          > until it reaches *either* the end of the array or a falsy value (e.g.
          > false, zero, undefined, etc). The second always loops until the end of
          > the array.

          (granted in this specific case the DOM API guarantees the array will
          not contain any falsy values, but the same is not true for many other
          arrays).
        • Philip Hutchison
          ... Wow, that s good to know. I hadn t considered that possibility, and if I had run into a falsy (killing the loop) it could have really screwed something
          Message 4 of 10 , Jan 16, 2009
          • 0 Attachment
            --- In jslint_com@yahoogroups.com, "Daniel Cassidy" <mail@...> wrote:
            >
            > 2009/1/15 Daniel Cassidy <mail@...>:
            > > As well as that, the two loops are *not* equivalent. The first loops
            > > until it reaches *either* the end of the array or a falsy value (e.g.
            > > false, zero, undefined, etc). The second always loops until the end of
            > > the array.
            >
            > (granted in this specific case the DOM API guarantees the array will
            > not contain any falsy values, but the same is not true for many other
            > arrays).
            >

            Wow, that's good to know. I hadn't considered that possibility, and
            if I had run into a falsy (killing the loop) it could have really
            screwed something up.

            Thanks for the tip!
          Your message has been successfully submitted and would be delivered to recipients shortly.