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

option.forin

Expand Messages
  • Douglas Crockford
    JSLint is much more rigorous now in looking at the filtering of for..in loops. It now expects to see one of these forms: for (name in object) { if
    Message 1 of 8 , Feb 23, 2011
    • 0 Attachment
      JSLint is much more rigorous now in looking at the filtering of for..in loops. It now expects to see one of these forms:

      for (name in object) {
      if (object[name] === ...) {
      ...
      }
      }

      for (name in object) {
      if (object[name] !== ...) {
      ...
      }
      }

      for (name in object) {
      if (typeof object[name] === ...) {
      ...
      }
      }

      for (name in object) {
      if (typeof object[name] !== ...) {
      ...
      }
      }

      for (name in object) {
      if (object.hasOwnProperty(name)) {
      ...
      }
      }

      for (name in object) {
      if (Object.prototype.hasOwnProperty(object, name)) {
      ...
      }
      }

      All other forms will produce a warning. There are other forms that
      are probably reasonable. If you know of one, please let me know and
      I will see to incorporating it.

      In the meantime, you can set option.forin to true.
    • John Hawkinson
      Thanks, Douglas, for the option.forin additions today. They look great. Embarassingly, right next to the unfiltered for/in in my code, I also had an unfiltered
      Message 2 of 8 , Feb 23, 2011
      • 0 Attachment
        Thanks, Douglas, for the option.forin additions today. They look great.

        Embarassingly, right next to the unfiltered for/in in my code,
        I also had an unfiltered 'in', which I broke down to this:

        for (i = wordlist.length - 1; i >= 0; i--) {
        w = wordlist[i];
        if (w in obj) {
        obj[w]++;
        }
        }

        which counts the frequency of words in wordlist, as long as they
        are already members of obj. So I guess bad things will happen
        if wordlist happened to contain "constructor."

        I'm of two minds here. Seems like jslint should look for unfiltered
        'in's even when they appear outside of 'for's. But maybe that's
        asking too much? I mean, JSLint can't catch everything...

        I assume the correct form here is just to test (obj.hasOwnProperty(w))
        instead of the 'in'.

        --jhawk@...
        John Hawkinson
      • Douglas Crockford
        ... This is a good argument for eliminating -in-. I have never found it to be useful, and you have identified a case where it is dangerous. In any case, there
        Message 3 of 8 , Feb 23, 2011
        • 0 Attachment
          --- In jslint_com@yahoogroups.com, John Hawkinson <jhawk@...> wrote:
          > Embarassingly, right next to the unfiltered for/in in my code,
          > I also had an unfiltered 'in', which I broke down to this:
          >
          > for (i = wordlist.length - 1; i >= 0; i--) {
          > w = wordlist[i];
          > if (w in obj) {
          > obj[w]++;
          > }
          > }
          >
          > which counts the frequency of words in wordlist, as long as they
          > are already members of obj. So I guess bad things will happen
          > if wordlist happened to contain "constructor."
          >
          > I'm of two minds here. Seems like jslint should look for unfiltered
          > 'in's even when they appear outside of 'for's. But maybe that's
          > asking too much? I mean, JSLint can't catch everything...
          >
          > I assume the correct form here is just to test (obj.hasOwnProperty(w))
          > instead of the 'in'.

          This is a good argument for eliminating -in-. I have never found it to be useful, and you have identified a case where it is dangerous. In any case, there are safer alternatives. Should we get rid of -in-?

          The test I would use for your application is (typeof obj === 'number').
        • John Hawkinson
          Douglas Crockford wrote on Thu, 24 Feb 2011 ... I assume you mean (typeof obj[w] === number )? Doesn t this present a problem if
          Message 4 of 8 , Feb 23, 2011
          • 0 Attachment
            Douglas Crockford <douglas@...> wrote on Thu, 24 Feb 2011
            at 01:56:28 -0000 in <ik4dsc+6uuo@...>:

            > > I assume the correct form here is just to test (obj.hasOwnProperty(w))
            > > instead of the 'in'.

            > The test I would use for your application is (typeof obj === 'number').

            I assume you mean (typeof obj[w] === 'number')?

            Doesn't this present a problem if someone sets

            Object.prototype.commonword = 37;

            where "commonword" is some common word?

            Or should I just assume "No one would be so stupid as to do that"?

            --jhawk@...
            John Hawkinson
          • Rob Richardson
            I agree -in- is quite dangerous, but how would one enumerate an object s properties without it? Rob ... useful, and you have identified a case where it is
            Message 5 of 8 , Feb 23, 2011
            • 0 Attachment
              I agree -in- is quite dangerous, but how would one enumerate an object's
              properties without it?

              Rob
              On Feb 23, 2011 6:56 PM, "Douglas Crockford" <douglas@...> wrote:
              > --- In jslint_com@yahoogroups.com, John Hawkinson <jhawk@...> wrote:
              >> Embarassingly, right next to the unfiltered for/in in my code,
              >> I also had an unfiltered 'in', which I broke down to this:
              >>
              >> for (i = wordlist.length - 1; i >= 0; i--) {
              >> w = wordlist[i];
              >> if (w in obj) {
              >> obj[w]++;
              >> }
              >> }
              >>
              >> which counts the frequency of words in wordlist, as long as they
              >> are already members of obj. So I guess bad things will happen
              >> if wordlist happened to contain "constructor."
              >>
              >> I'm of two minds here. Seems like jslint should look for unfiltered
              >> 'in's even when they appear outside of 'for's. But maybe that's
              >> asking too much? I mean, JSLint can't catch everything...
              >>
              >> I assume the correct form here is just to test (obj.hasOwnProperty(w))
              >> instead of the 'in'.
              >
              > This is a good argument for eliminating -in-. I have never found it to be
              useful, and you have identified a case where it is dangerous. In any case,
              there are safer alternatives. Should we get rid of -in-?
              >
              > The test I would use for your application is (typeof obj === 'number').
              >


              [Non-text portions of this message have been removed]
            • Douglas Crockford
              ... I was talking about the -in- infix operator. This confusion is another justification for getting rid of it.
              Message 6 of 8 , Feb 23, 2011
              • 0 Attachment
                --- In jslint_com@yahoogroups.com, Rob Richardson <erobrich@...> wrote:

                > I agree -in- is quite dangerous, but how would one enumerate an object's
                > properties without it?

                I was talking about the -in- infix operator. This confusion is another justification for getting rid of it.
              • bluflonalgul
                I think there is this case where the for in loops on a object used as a record. Does it makes sense here to had such tests mentioned in your option.forin ?
                Message 7 of 8 , Feb 24, 2011
                • 0 Attachment
                  I think there is this case where the 'for in' loops on a object used as a record.
                  Does it makes sense here to had such tests mentioned in your option.forin ?

                  tools={
                  steps: {
                  init: {desc: "blabla", time: 1 },
                  done: {desc: "blabla", time: 0 },
                  fast: {desc: "blabla", time: 0 },
                  },

                  // setCurrentStep(step) sets time to a timestamp : steps[step].time= 6544654

                  getCurrentStep: function getCurrentStep(){
                  var curStep=null;
                  var maxTime=0;
                  for ( var step in tools.steps ){
                  if ( maxTime < tools.steps[step].time ){
                  maxTime= tools.steps[step].time;
                  curStep= step;
                  }
                  }
                  return curStep;
                  },

                  }
                • Douglas Crockford
                  ... Yes, because some third party code could insert something into Object.prototype without your knowledge and approval. So program defensively, so that your
                  Message 8 of 8 , Feb 24, 2011
                  • 0 Attachment
                    --- In jslint_com@yahoogroups.com, "bluflonalgul" <bluflonalgul@...> wrote:

                    > I think there is this case where the 'for in' loops on a object used as a record.
                    > Does it makes sense here to had such tests mentioned in your option.forin ?


                    Yes, because some third party code could insert something into Object.prototype without your knowledge and approval. So program defensively, so that your code will always perform correctly.
                  Your message has been successfully submitted and would be delivered to recipients shortly.