kangax: I was thinking the same earlier (about caching the
hasOwnProperty), when I realized that my method incorrectly doesn't
allow functions to be passed (not sure about Prototype's original
one).  I've updated the function on my blog to:

Object.keys = Object.keys || function (o) {
    if (typeof o != "object" && typeof o != "function" || o === null)
        throw new TypeError("Object.keys called on a non-object");

    var result = [], hop = Object.prototype.hasOwnProperty;
    for (var name in o) {
        if (hop.call(o, name))
            result.push(name);
    }
    return result;
};

Regarding the DontEnum bug, you could work around that by checking
them explicitly hasOwnProperty outside of the for...in loop.

On Oct 21, 2:39 pm, kangax <kan...@gmail.com> wrote:
> On Oct 14, 4:45 pm, Andy E <andyearns...@gmail.com> wrote:
>
>
>
>
>
> > I recently discovered that several compatibility implementations of
> > Object.keys() on the web will throw an error for DOM objects in
> > Internet Explorer 8 and lower.  This differs from the current browsers
> > having a native implementation and how the ECMAScript 5th edition
> > defines the behaviour of Object.keys.
>
> > The problem lies with the hasOwnProperty() check, which is not a valid
> > method on Internet Explorer DOM objects because they aren't instances
> > of Object.  The fix is very simple; "borrow"  hasOwnProperty() from
> > Object.prototype.hasOwnProperty() instead.  The fixed PrototypeJS
> > implementation would be:
>
> >   function keys(object) {
> >     if (Type(object) !== OBJECT_TYPE) { throw new TypeError(); }
> >     var results = [];
> >     for (var property in object) {
> >       if (object.prototype.hasOwnProperty.call(object, property)) {
> >         results.push(property);
> >       }
> >     }
> >     return results;
> >   }
>
> No need to access `hasOwnProperty` off of `Object.prototype` on _every
> iteration_, making things unnecessary slow. It's easy enough to alias
> `hasOwnProperty` (if it isn't already, somewhere there in the source).
>
> As a side note, this implementation (as well Prototype's original one)
> doesn't take care of JScript's DontEnum bug (<IE9, IIRC), which means
> `toString`, `valueOf`, etc. -named keys won't be returned. So that's
> something else to keep in mind as far as ES5-compatibility goes.
>
> --
> kangax

-- 
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to 
prototype-core-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en

Reply via email to