pvorb / clone

deeply clone arbitrary objects in javascript
https://www.npmjs.com/package/clone
MIT License
781 stars 129 forks source link

fix: copy writable prototype props #88

Open vkarpov15 opened 6 years ago

vkarpov15 commented 6 years ago

Hi,

Looks like #36 introduced a bug where prototype properties that don't have a setter won't get copied, even if they are writable. Example:

function X() {
  this.test = 'A';
}

Object.defineProperty(X.prototype, 'test', {
  configurable: false,
  writable: true,
  enumerable: true
});

// "X { test: 'A' }"
console.log(new X());
// "X {}", `test` prop omitted!
console.log(clone(new X()));

This issue came up in Automattic/mongoose#5896

Thanks for the great lib and happy holidays!

olsonpm commented 6 years ago

Just in case it's not clear, mdn defines descriptors as either being "data" or "accessor" descriptors. The current code assumes only the latter whereas both need to be accounted for.

A data descriptor also has the following optional keys: value... writeable

An accessor descriptor also has the following optional keys: get... set