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

Option to tolerate var-declaration in for-loops

Expand Messages
  • maddinr92
    First, thank you for your great validation tool. Demo-JavaScript: var test = function () { var letsHaveSomeFun = {}; for (var entry in letsHaveSomeFun) {
    Message 1 of 2 , Sep 2, 2011
    View Source
    • 0 Attachment
      First, thank you for your great validation tool.

      Demo-JavaScript:

      var test = function () {
      var letsHaveSomeFun = {};

      for (var entry in letsHaveSomeFun) {
      alert("do anything");
      }
      };

      If I try to validate this coding, I get following errors:

      Problem at line 6 character 10: Move 'var' declarations to the top of the function.
      for (var entry in letsHaveSomeFun) {
      Problem at line 6 character 10: Stopping. (57% scanned).

      Now I would need an option to skip this 2 errors.
      Is there any chance to get them?

      Thank you.
      Martin
    • Douglas Crockford
      ... No. Declare all vars at the top of the function to avoid hoisting confusions. This is particularly import with the for statement because of the capture of
      Message 2 of 2 , Sep 2, 2011
      View Source
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "maddinr92" <martin.riedl.92@...> wrote:
        >
        > First, thank you for your great validation tool.
        >
        > Demo-JavaScript:
        >
        > var test = function () {
        > var letsHaveSomeFun = {};
        >
        > for (var entry in letsHaveSomeFun) {
        > alert("do anything");
        > }
        > };
        >
        > If I try to validate this coding, I get following errors:
        >
        > Problem at line 6 character 10: Move 'var' declarations to the top of the function.
        > for (var entry in letsHaveSomeFun) {
        > Problem at line 6 character 10: Stopping. (57% scanned).
        >
        > Now I would need an option to skip this 2 errors.
        > Is there any chance to get them?

        No. Declare all vars at the top of the function to avoid hoisting confusions. This is particularly import with the for statement because of the capture of the induction variable.
      Your message has been successfully submitted and would be delivered to recipients shortly.