Permalink
Browse files

Merge pull request #200 from ljharb/object_keys_es3_enumerability

Object keys es3 enumerability. Fixes #155, relates to #198/#83.
  • Loading branch information...
2 parents 847e164 + 93dd364 commit ee416a685fe37af25e2ddd6a7b1b0dddc4f05213 @ljharb ljharb committed Jan 5, 2014
Showing with 12 additions and 8 deletions.
  1. +11 −8 es5-shim.js
  2. +1 −0 tests/spec/s-object.js
View
@@ -655,6 +655,7 @@ if (!Array.prototype.lastIndexOf || ([0, 1].lastIndexOf(0, -3) != -1)) {
if (!Object.keys) {
// http://whattheheadsaid.com/2010/10/a-safer-object-keys-compatibility-implementation
var hasDontEnumBug = true,
+ hasProtoEnumBug = (function () {}).propertyIsEnumerable('prototype'),
dontEnums = [
"toString",
"toLocaleString",
@@ -671,25 +672,27 @@ if (!Object.keys) {
}
Object.keys = function keys(object) {
+ var isFunction = toString.call(object) === '[object Function]',
@ashi009

ashi009 Jan 7, 2014

toString.call() is not supported on IE8 (ref). How about _toString()?

@ljharb

ljharb Jan 7, 2014

Owner

Thanks! Fixed in 5b0ffb6

+ isObject = object !== null && typeof object === 'object';
- if (
- (typeof object != "object" && typeof object != "function") ||
- object === null
- ) {
+ if (!isObject && !isFunction) {
throw new TypeError("Object.keys called on a non-object");
}
- var keys = [];
+ var keys = [],
+ skipProto = hasProtoEnumBug && isFunction;
for (var name in object) {
- if (owns(object, name)) {
+ if (!(skipProto && name === 'prototype') && owns(object, name)) {
keys.push(name);
}
}
if (hasDontEnumBug) {
- for (var i = 0, ii = dontEnumsLength; i < ii; i++) {
+ var ctor = object.constructor,
+ skipConstructor = ctor && ctor.prototype === object;
+ for (var i = 0; i < dontEnumsLength; i++) {
var dontEnum = dontEnums[i];
- if (owns(object, dontEnum)) {
+ if (!(skipConstructor && dontEnum === 'constructor') && owns(object, dontEnum)) {
keys.push(dontEnum);
}
}
View
@@ -53,6 +53,7 @@ describe('Object', function () {
var Foo = function () {};
expect(Object.keys(Foo.prototype)).toEqual([]);
expect(Object.keys(Boolean.prototype)).toEqual([]);
+ expect(Object.keys(Object.prototype)).toEqual([]);
});
});

0 comments on commit ee416a6

Please sign in to comment.