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

352Re: expected a conditional statement and instead saw an assignment

Expand Messages
  • Philip Hutchison
    Jan 10, 2009
    • 0 Attachment
      --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...>
      wrote:
      >
      > --- In jslint_com@yahoogroups.com, "montago_2004" <mdk@> wrote:
      > >
      > > --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@>
      > > > > var arr = [];
      > > > >
      > > > > for(var i, b; b=arr[i] ; i++){
      > > > >
      > > > > }
      > >
      > >
      > > the option doesn't apply to the for-loop... the "error" is still there
      >
      > You should write either
      >
      > for(var i, b; b == arr[i] ; i++){
      >
      > or
      >
      > for(var i, b; (b = arr[i]); i++){
      >
      > The way you have written it looks like a probable error.
      >


      Hi guys

      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';
      ...
      }

      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
    • Show all 10 messages in this topic