6

Friends,

I notice in Firefox v23.0.1 that, hasOwnProperty of HTMLElement(input,button..etc) doesn't work,

button1.hasOwnProperty('id') = false

I use for in to check:

 var str1 = '';
        for (pp in button1) {
            if (button1.hasOwnProperty(pp)) {
                str1 += (',' + pp);
            }
        }
        alert(str1);//nothing here

but in chrome hasOwnProperty works well.

do you know is it a bug?

Praveen
  • 55,303
  • 33
  • 133
  • 164
Lindy
  • 287
  • 5
  • 13
  • Test a bit more widely and you'll find browsers in use that don't support *hasOwnProperty* on DOM objects, nor do they implement any kind of inheritance. – RobG Aug 28 '13 at 05:09

1 Answers1

5

Per spec, the "id" property is on either HTMLElement.prototype or Element.prototype (depending on the spec version).

Firefox gets this right. Chrome puts all properties directly on objects instead.


http://dev.w3.org/2006/webapi/WebIDL/#es-attributes http://dev.w3.org/2006/webapi/WebIDL/#ecmascript-binding
plalx
  • 42,889
  • 6
  • 74
  • 90
Boris Zbarsky
  • 34,758
  • 5
  • 52
  • 55
  • Are you sure? Properties like id make no sense on the prototype. I'd guess they're just not enumerable on Firefox (not sure if per the DOM spec or not). – bfavaretto Aug 28 '13 at 04:47
  • 1
    Per which spec? Always good to say which one, since DOM specs until recently were language agnostic. And you should always backup such statements with appropriate references. – RobG Aug 28 '13 at 05:07
  • I agree with @Bergi. That's not possible. If the `id` would be on the `prototype` it would be shared between every instances, which doesn't make any sense. – plalx Aug 28 '13 at 11:26
  • @bfavaretto I'm very sure. http://dev.w3.org/2006/webapi/WebIDL/#es-attributes defines it as an accessor property (with a getter and setter) on the prototype. The getter knows how to get the value out of the instance. This allows scripts to override property getters or setters on all objects of a given type at once if needed for polyfills. – Boris Zbarsky Aug 28 '13 at 14:17
  • @RobG Per the WebIDL spec, which actually defines the JS reflection of the IDL in meticulous detail. See http://dev.w3.org/2006/webapi/WebIDL/#ecmascript-binding in general and http://dev.w3.org/2006/webapi/WebIDL/#es-attributes in particular – Boris Zbarsky Aug 28 '13 at 14:19
  • @plaxl It's quite possible; you're thinking of a value property, while "id" is an accesor property. Not only is it possible, but it has been implemented for years by both Gecko (Firefox) and Trident (IE). – Boris Zbarsky Aug 28 '13 at 14:20
  • @bfavaretto Note that Element.prototype.hasOwnProperty("id") returns true in recent Firefox, by the way, so there is no need to guess at anything: the property is right there. – Boris Zbarsky Aug 28 '13 at 14:22
  • Thanks for the link, you're right. I didn't know attributes were mapped to accessors. – bfavaretto Aug 28 '13 at 14:28
  • I haven't tought about this when I wrote this comment. Thanks! I added the references to your answer... it allowed me to remove my erroneous downvote as well. – plalx Aug 28 '13 at 17:33
  • Now, Chrome does not work either, tested in version 53 for Mac. – Gustavo May 05 '17 at 02:10
  • Right, they fixed their bug and are now following the spec. – Boris Zbarsky May 05 '17 at 13:57