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

for in ... hasOwnProperty

Expand Messages
  • Jacob Davenport
    Upon your advice (and one painful experience), I write my for in statements like this: for (var listName in listItems) if
    Message 1 of 3 , Jul 31, 2008
    • 0 Attachment
      Upon your advice (and one painful experience), I write my "for in"
      statements like this:

      for (var listName in listItems) if (listItems.hasOwnProperty(listName)) {
      // do stuff
      }

      I know that the "for" statement does not have the normal braces with
      this syntax. I see in your book and on your web pages that you suggest:

      for (var listName in listItems) {
      if (listItems.hasOwnProperty(listName)) {
      // do stuff
      }
      }

      The second form is clearly more standard, and I'm happy to use it.

      Here's my question: am I crazy, or did JSLint at one point accept the
      first form without complaint?
    • Michael Newton
      ... (listItems.hasOwnProperty(listName)) { ... The documentation says JSLint expects blocks with function, if, switch, while, for, do, and try statements and
      Message 2 of 3 , Jul 31, 2008
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "Jacob Davenport" <jacob@...> wrote:
        >
        > Upon your advice (and one painful experience), I write my "for in"
        > statements like this:
        >
        > for (var listName in listItems) if
        (listItems.hasOwnProperty(listName)) {
        > // do stuff
        > }
        >
        > I know that the "for" statement does not have the normal braces with
        > this syntax. I see in your book and on your web pages that you suggest:
        >
        > for (var listName in listItems) {
        > if (listItems.hasOwnProperty(listName)) {
        > // do stuff
        > }
        > }
        >
        > The second form is clearly more standard, and I'm happy to use it.
        >
        > Here's my question: am I crazy, or did JSLint at one point accept the
        > first form without complaint?
        >

        The documentation says "JSLint expects blocks with function, if,
        switch, while, for, do, and try statements and nowhere else. An
        exception is made for an unblocked if statement on an else or for in."

        So it looks like it should be okay.
      • Douglas Crockford
        ... Yes. At one time I was experimenting with that form. I was later persuaded that it was a bad for so I took it out. I am constantly experimenting with
        Message 3 of 3 , Jul 31, 2008
        • 0 Attachment
          --- In jslint_com@yahoogroups.com, "Jacob Davenport" <jacob@...> wrote:

          > Here's my question: am I crazy, or did JSLint at one point accept the
          > first form without complaint?

          Yes. At one time I was experimenting with that form. I was later
          persuaded that it was a bad for so I took it out.

          I am constantly experimenting with style, trying to refine forms that
          are the most trustworthy and resilient for this sloppy language. So
          from time to time I change my mind about the best forms. I am still
          learning, or trying to.
        Your message has been successfully submitted and would be delivered to recipients shortly.