1

It seems that Object.assign does not assign properties that are not enumerable.

var weasel = {test: 1};
Object.defineProperty(weasel,
          'isFetching',
          {
            value: true,
            writable: true,
            enumerable: false
          })
Object {test: 1, isFetching: true}
Object.assign({}, weasel);
Object {test: 1}

Is there a way around this?

Ryan-Neal Mes
  • 6,003
  • 7
  • 52
  • 77
  • 1
    @Andreas You probably want `Object.create(Object.prototype, …)` though - or even with `Object.getPrototypeOf(weasel)`. Or use `Object.defineProperties({}, Object.getOwnPropertyDescriptors(weasel))` instead – Bergi Oct 17 '16 at 12:19
  • Check this http://stackoverflow.com/questions/8024149/is-it-possible-to-get-the-non-enumerable-inherited-property-names-of-an-object/8024294#8024294 – Netham Oct 17 '16 at 12:31

1 Answers1

0

You can use this one liner:

x=Object.getOwnPropertyNames(weasel).reduce((a,b)=>(Object.defineProperty(a,b,Object.getOwnPropertyDescriptor(weasel,b)),a),{})
James Wakefield
  • 526
  • 3
  • 11
  • @Andreas `Object.assign` doesn't either? OP only asked to work around non-enumerability, not to copy descriptors – Bergi Oct 17 '16 at 12:18
  • I screwed this around to work... I suppose I will make it look pretty. This doesn't clone the prototype but then all you'd need to do to achieve that is use object.create – James Wakefield Oct 17 '16 at 12:27