clone icon indicating copy to clipboard operation
clone copied to clipboard

fix: copy writable prototype props

Open vkarpov15 opened this issue 7 years ago • 1 comments

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!

vkarpov15 avatar Dec 21 '17 00:12 vkarpov15

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

olsonpm avatar Apr 17 '18 21:04 olsonpm