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

2573Re: Possible bug in reporting 'Bad for in variable'

Expand Messages
  • pierremartineau
    Aug 22, 2011
    • 0 Attachment
      --- In jslint_com@yahoogroups.com, "Jerry" <jluke@...> wrote:
      >
      >
      > --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@> wrote:
      > >
      > > --- In jslint_com@yahoogroups.com, "Jerry" <jluke@> wrote:
      > > >
      > > > I saw this posted in a previous message, but the thread ended because the provided code sample did not reproduce the bug. I was able to produce the following code sample that reproduces the 'Bad for in variable' problem:
      > > > =======================================================
      > > > function badForInVar() {
      > > > var basicData = { 'Status' : 'NORMAL' , 'Priority' : 'High' },
      > > > prop;
      > > >
      > > > function saveBasicData(value){
      > > > var lookup = {};
      > > > lookup.displayName = value;
      > > > lookup.type = prop;
      > > > }
      > > > for (prop in basicData) {
      > > > if (basicData.hasOwnProperty(prop)) {
      > > > saveBasicData(basicData[prop]);
      > > > }
      > > > }
      > > > }
      > > > =======================================================
      > > > I believe the saveBasicData function declaration between the var declaration of 'prop' and its usage in the for/in loop is contributing to JSLint reporting the 'Bad for in variable' error. If I move the saveBasicData function to follow the for/in loop, I no longer get the error (but instead get the message: 'saveBasicData' was used before it was defined).
      > >
      > >
      > > Thank you. That was very helpful. Please try it now.
      > >
      > Boy, that was quick. Thanks, it works perfectly now.
      >

      I believe this is a similar problem:

      with these options:
      /*jslint es5: true, indent: 2 */

      JSLint objects:
      "Bad for in variable 'a'."

      on this code:

      var a, b = {}, test;
      test = function () {
      'use strict';
      for (a in b) {
      if (b.hasOwnProperty(a)) {
      // do anything
      return true;
      }
      }
      };

      but not on this code:

      var a, b = {}, test;
      test = function () {
      'use strict';
      var a;
      for (a in b) {
      if (b.hasOwnProperty(a)) {
      // do anything
      return true;
      }
      }
      };
    • Show all 8 messages in this topic