Created
March 3, 2014 01:39
-
-
Save WebReflection/9317065 to your computer and use it in GitHub Desktop.
A plural ES5 + ES6 friendly version of Object.getOwnPropertyDescriptor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'getOwnPropertyDescriptors' in Object || ( | |
Object.getOwnPropertyDescriptors = function (Object) { | |
var | |
gOPD = Object.getOwnPropertyDescriptor, | |
gOPN = Object.getOwnPropertyNames, | |
gOPS = Object.getOwnPropertySymbols, | |
gNS = gOPS ? function (object) { | |
return gOPN(object).concat(gOPS(object)); | |
} : | |
gOPN, | |
descriptors | |
; | |
function copyFrom(key) { | |
descriptors[key] = gOPD(this, key); | |
} | |
return function getOwnPropertyDescriptors(object) { | |
var result = descriptors = {}; | |
gNS(object).forEach(copyFrom, object); | |
descriptors = null; | |
return result; | |
}; | |
}(Object) | |
); |
A possible Object.copy
that works like Object.assign
but includes getters, setters, and non enumerable properties too.
Object.copy = (function (O) {
var
dP = O.defineProperty,
gOPD = O.getOwnPropertyDescriptor,
gOPN = O.getOwnPropertyNames,
gOPS = O.getOwnPropertySymbols,
set = function (target, source) {
for (var
key,
keys = gOPN(source).concat(gOPS(source)),
i = 0,
l = keys.length; i < l; i++
) {
key = keys[i];
dP(target, key, gOPD(source, key));
}
}
;
return function copy(target) {
for (var i = 1, l = arguments.length; i < l; i++) {
set(target, arguments[i]);
}
return target;
};
}(Object));
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An alternate polyfillable ES6 version that relies on
Reflect.ownKeys
,Array#reduce
, andObject.getOwnPropertyDescriptor
:(just posting this comment here so I don't forget it later ;-) )