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

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

Expand Messages
  • Daniel Cassidy
    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.
    • Show all 10 messages in this topic