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

In IE some objects lack hasOwnProperty?

Expand Messages
  • Erik Eckhardt
    In IE 7 I tried using hasOwnProperty on document.styleSheets[0].rules[0] and found out that the rule object returned from the expression doesn t have that
    Message 1 of 2 , Apr 4, 2011
    • 0 Attachment
      In IE 7 I tried using hasOwnProperty on document.styleSheets[0].rules[0] and
      found out that the rule object returned from the expression doesn't have
      that function!

      I noticed in jQuery a clever way to get around this using call on
      Object.hasOwnProperty. However, jslint doesn't recognize this as valid
      filtering.

      Would you suggest how to handle this problem? Here's some sample code:

      var key,

      obj = {

      a: 123,

      b: 456

      };



      for (key in obj) {

      if (obj[key] !== undefined) {

      alert(key + ': ' + obj[key].toString());

      }

      }



      for (key in obj) {

      if (obj.hasOwnProperty ? obj.hasOwnProperty(key) :
      Object.hasOwnProperty.call(obj, key)) {

      alert(key + ': ' + obj[key].toString());

      }

      }



      for (key in obj) {

      if (Object.hasOwnProperty.call(obj, key)) {

      alert(key + ': ' + obj[key].toString());

      }
      }

      Using the comparison to undefined doesn't seem like it would really filter
      out items that are in the prototype chain.

      The second way of doing it seems unnecessarily clunky. Is there merit in
      simply using the final method, defaulting to call? And should it be Object.
      or {}. ?

      I'm also confused about why jslint is complaining "Problem at line 13
      character 6: Bad for in variable 'key'." Could I get help with that?


      [Non-text portions of this message have been removed]
    • Douglas Crockford
      ... for (key in object) { if (Object.prototype.hasOwnProperty.call(object, key)) { ... } } hasOwnProperty should have been an operator, not a method, because
      Message 2 of 2 , Apr 4, 2011
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, Erik Eckhardt <erik@...> wrote:
        >
        > In IE 7 I tried using hasOwnProperty on document.styleSheets[0].rules[0] and
        > found out that the rule object returned from the expression doesn't have
        > that function!
        >
        > I noticed in jQuery a clever way to get around this using call on
        > Object.hasOwnProperty. However, jslint doesn't recognize this as valid
        > filtering.
        >
        > Would you suggest how to handle this problem?

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

        hasOwnProperty should have been an operator, not a method, because being a method, it is prone to these sorts of problems. But it is what it is, so you have to work around that.
      Your message has been successfully submitted and would be delivered to recipients shortly.