-
Notifications
You must be signed in to change notification settings - Fork 5
/
object.js
63 lines (59 loc) · 1.54 KB
/
object.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
Object.defineProperty(Object, 'keys', {
configurable: true,
enumerable: false,
writable: true,
value: function(obj) {
if (typeof obj !== 'function' && (typeof obj !== 'object' || !obj)) {
throw TypeError('Object.keys called on non-object');
}
var keys = [];
for (var prop in obj) {
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
keys.push(prop);
}
}
return keys;
}
});
Object.defineProperty(Object, 'defineProperties', {
configurable: true,
enumerable: false,
writable: true,
value: function(obj, props) {
if (typeof obj !== 'function' && (typeof obj !== 'object' || !obj)) {
throw TypeError('Object.defineProperties called on non-object');
}
if (props === undefined || props === null) {
throw TypeError('Cannot convert undefined or null to object');
}
props = Object(props);
var keys = Object.keys(props);
for (var i = 0; i < keys.length; i++) {
Object.defineProperty(obj, keys[i], props[keys[i]]);
}
return obj;
}
});
Object.defineProperty(Object.prototype, 'isPrototypeOf', {
configurable: true,
enumerable: false,
writable: true,
value: function(obj) {
var child = Object.getPrototypeOf(obj);
while (child) {
if (child == this) {
return true;
}
child = Object.getPrototypeOf(child);
}
return false;
}
});
Object.defineProperty(Object.prototype, 'toLocaleString', {
configurable: true,
enumerable: false,
writable: true,
value: function() {
return this.toString();
}
});