pvorb / clone

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

Not cloning object getters and setters as expected #121

Open bertolo1988 opened 1 year ago

bertolo1988 commented 1 year 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 1 year 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 11 months ago

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