Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Object keys es3 enumerability #200

Merged
merged 3 commits into from

1 participant

@ljharb
Owner

Fixes #155, relates to #198/#83.

@ljharb ljharb merged commit ee416a6 into es-shims:master

1 check passed

Details default The Travis CI build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 5, 2014
  1. @ljharb

    Adding some test cases.

    ljharb authored
  2. @ljharb

    Handling prototype enum bug.

    ljharb authored
  3. @ljharb
This page is out of date. Refresh to see the latest.
Showing with 12 additions and 8 deletions.
  1. +11 −8 es5-shim.js
  2. +1 −0  tests/spec/s-object.js
View
19 es5-shim.js
@@ -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]',
+ 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
1  tests/spec/s-object.js
@@ -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([]);
});
});
Something went wrong with that request. Please try again.