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