hasOwnProperty HTMLElement Firefox
Asked Answered
L

1

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?

Leacock answered 28/8, 2013 at 3:27 Comment(1)
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.Perl
S
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
Substation answered 28/8, 2013 at 4:5 Comment(11)
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).Tinhorn
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.Perl
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.Antirrhinum
@Tinhorn I'm very sure. 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.Substation
@Perl Per the WebIDL spec, which actually defines the JS reflection of the IDL in meticulous detail. See dev.w3.org/2006/webapi/WebIDL/#ecmascript-binding in general and dev.w3.org/2006/webapi/WebIDL/#es-attributes in particularSubstation
@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).Substation
@Tinhorn 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.Substation
Thanks for the link, you're right. I didn't know attributes were mapped to accessors.Tinhorn
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.Antirrhinum
Now, Chrome does not work either, tested in version 53 for Mac.Cutaneous
Right, they fixed their bug and are now following the spec.Substation

© 2022 - 2024 — McMap. All rights reserved.