Created
November 27, 2017 18:50
Star
You must be signed in to star a gist
Copy the specified enumerable properties (strings or symbols) from the source object to the destination object
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
function assignProps(source, dest, props) { | |
const descriptors = {}; | |
props.forEach(name => { | |
let desc = Object.getOwnPropertyDescriptor(source, name); | |
if (desc && desc.enumerable) { | |
descriptors[name] = desc; | |
} | |
}); | |
Object.defineProperties(dest, descriptors); | |
} | |
const a = Symbol('a'); | |
const one = {[a]: 'alpha', b: 'beta', z: 'omega'}; | |
Object.defineProperty(one, 'c', { value: 'C', enumerable: false }) | |
const two = {b: 2, c: 3}; | |
assignProps(one, two, [a, 'b', 'c']) | |
console.log(two) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment