function calls

Solutions on MaxInterview for function calls by the best coders in the world

showing results for - "function calls"
Isabell
14 Jul 2020
1const obj = {
2  foo: 1,
3  get bar() {
4    return 2;
5  }
6};
7
8let copy = Object.assign({}, obj); 
9console.log(copy); 
10// { foo: 1, bar: 2 }
11// The value of copy.bar is obj.bar's getter's return value.
12
13// This is an assign function that copies full descriptors
14function completeAssign(target, ...sources) {
15  sources.forEach(source => {
16    let descriptors = Object.keys(source).reduce((descriptors, key) => {
17      descriptors[key] = Object.getOwnPropertyDescriptor(source, key);
18      return descriptors;
19    }, {});
20    
21    // By default, Object.assign copies enumerable Symbols, too
22    Object.getOwnPropertySymbols(source).forEach(sym => {
23      let descriptor = Object.getOwnPropertyDescriptor(source, sym);
24      if (descriptor.enumerable) {
25        descriptors[sym] = descriptor;
26      }
27    });
28    Object.defineProperties(target, descriptors);
29  });
30  return target;
31}
32
33copy = completeAssign({}, obj);
34console.log(copy);
35// { foo:1, get bar() { return 2 } }
36