pvorb / clone

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

Not cloning object getters and setters as expected #121

Open bertolo1988 opened 2 years ago

bertolo1988 commented 2 years ago

The following tests should not fail:

    it('should clone a getter', () => {
      const input = {
        a: 2,
        get foo() {
          return this.a * 2
        }
      }
      let clone = func(input)
      assert.ok(clone.foo === 4)
      assert.ok(
        typeof Object.getOwnPropertyDescriptor(clone, 'foo').get === 'function'
      )
    })

   it('should clone a setter', () => {
      const input = {
        set foo(val) {
          this.a = val
        }
      }
      let clone = func(input)
      clone.foo('bar')
      assert.ok(clone.a === 'bar')
      assert.ok(input.a === undefined)
      assert.ok(
        typeof Object.getOwnPropertyDescriptor(clone, 'foo').set === 'function'
      )
    })
pvorb commented 2 years ago

This lib was written back when these methods did not exist (I believe).

I don't have time to actively maintain it. So you might want to look for alternatives that support this.

jimmywarting commented 1 year ago

could also be worth mentioning that there also is a built in structuredClone() that do also support getters.