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

1744Re: [jslint] Move all var declarations to the top of the function and unable to continue.

Expand Messages
  • Douglas Crockford
    Jan 10, 2011
    • 0 Attachment
      JavaScript does not have block scope. It only has function scope. So it
      is wise to always declare at the top.

      Insert the braces, then it won't stop.

      On 1/10/2011 1:27 AM, Luke Page wrote:
      > Is it purposeful or a bug that when the onevar option is off it is
      > complaining about vars needing to be moved to the top of the function if
      > they appear in a for loop declaration, but not if they don't. e.g.
      >
      >
      > var func = function() {
      > while(true) {
      > alert("test 1");
      > }
      > var i = 0; // no error
      > i++;
      > };
      >
      > var func = function() {
      > while(true) {
      > alert("test 2");
      > }
      > for(var i = 0; i< 9; i++) { //line 13 - error
      > alert("test3");
      > }
      > };
      >
      > *Error:*
      >
      > Problem at line 13 character 7: Move all 'var' declarations to the top of
      > the function.
      >
      > for(var i = 0; i< 9; i++) {
      >
      > Problem at line 13 character 7: Stopping, unable to continue. (81% scanned).
      >
      > If it is now a problem to use var in a for declaration, could the error be
      > more specific?
      >
      > Also JSLint now seems to be stopping on a lot of errors rather than
      > continuing so more than one error can be fixed at once.Is it possible to
      > change JSLint to the way it used to work, so that if(a) b; (e.g. missing
      > curly brackets) is not a stopping error?
      >
      >
      > [Non-text portions of this message have been removed]
      >
      >
      >
      > ------------------------------------
      >
      > Yahoo! Groups Links
      >
      >
      >
      >
    • Show all 4 messages in this topic