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

expected a conditional statement and instead saw an assignment

Expand Messages
  • montago_2004
    var arr = []; for(var i, b; b=arr[i] ; i++){ } //---------- according to : http://userjs.org/help/tutorials/efficient-code#fastloops its fully legit to make an
    Message 1 of 10 , Jul 27 2:08 AM
    • 0 Attachment
      var arr = [];

      for(var i, b; b=arr[i] ; i++){

      }

      //----------

      according to :
      http://userjs.org/help/tutorials/efficient-code#fastloops

      its fully legit to make an assignment inside the for-expression, this
      in order to speed up the execution of the script.

      Unless some modern browser won't execute the script, there's no reason
      to warn about it.

      I do know, that the error occur because the parser expects something
      and sees another - purhaps the available statements need an upgrade :-)


      of cause - such loops should be replaced by

      var i = arr.length
      do{
      var b = arr[i]
      }while(--i)

      which is much faster !
    • Douglas Crockford
      ... The code is faulty because it can enumerate inherited methods as well as data values. This is known to cause programs to fail. You can control this with
      Message 2 of 10 , Jul 27 4:19 AM
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "montago_2004" <mdk@...> wrote:
        >
        > var arr = [];
        >
        > for(var i, b; b=arr[i] ; i++){
        >
        > }
        >
        > //----------
        >
        > according to :
        > http://userjs.org/help/tutorials/efficient-code#fastloops
        >
        > its fully legit to make an assignment inside the for-expression, this
        > in order to speed up the execution of the script.
        >
        > Unless some modern browser won't execute the script, there's no reason
        > to warn about it.

        The code is faulty because it can enumerate inherited methods as well
        as data values. This is known to cause programs to fail.

        You can control this with the Tolerate unfiltered for in option.
      • montago_2004
        ... the option doesn t apply to the for-loop... the error is still there
        Message 3 of 10 , Jul 27 2:02 PM
        • 0 Attachment
          --- 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
        • Douglas Crockford
          ... 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.
          Message 4 of 10 , Jul 27 2:12 PM
          • 0 Attachment
            --- 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.
          • Philip Hutchison
            ... 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
            Message 5 of 10 , 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
            • Andy Stevens
              ... 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
              Message 6 of 10 , Jan 10, 2009
              • 0 Attachment
                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
              • 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 7 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 8 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 9 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 10 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.