On Oct 21, 12:11 pm, Andy E <andyearns...@gmail.com> wrote:
> 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;

That's better, but nothing is stopping from taking it a step further,
aliasing `hasOwnProperty` outside of the method.

>     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.

Exactly ;)

See for example `Object.forIn()` that I experimented with a couple of
years ago (wow, how time flies...) —
http://github.com/kangax/protolicious/blob/master/experimental/object.for_in.js#L18

Looking at the code now, I would simplify it slightly, but the idea
holds.

[...]

--
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