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

I've outlined the issue in more detail on my blog:
http://whattheheadsaid.com/2010/10/a-safer-object-keys-compatibility-implementation.

Cheers,

Andy E

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