Skip to content

Instantly share code, notes, and snippets.

@dotproto
Created November 27, 2017 18:50
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dotproto/35ec3d7f228b9f3dffa6971d87ea4f40 to your computer and use it in GitHub Desktop.
Copy the specified enumerable properties (strings or symbols) from the source object to the destination object
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