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

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

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