-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObject.keys.js
45 lines (40 loc) · 1.43 KB
/
Object.keys.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*globals define*/
// As a shim plugin, this breaks normal modularity by altering globals for sake of enabling standards on non-supporting browsers
// Adapted from https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/keys
define(function () {
'use strict';
if (Object.keys) { // Better to use shim plugin to detect presence, as reusable for other shims
return Object.keys;
}
var hasOwnProperty = Object.prototype.hasOwnProperty,
hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
],
dontEnumsLength = dontEnums.length;
return function (obj) {
var i, prop, result = [];
if ((typeof obj !== 'object' && typeof obj !== 'function') || obj === null) {
throw new TypeError('Object.keys called on non-object');
}
for (prop in obj) {
if (hasOwnProperty.call(obj, prop)) {
result.push(prop);
}
}
if (hasDontEnumBug) {
for (i=0; i < dontEnumsLength; i++) {
if (hasOwnProperty.call(obj, dontEnums[i])) {
result.push(dontEnums[i]);
}
}
}
return result;
};
});