clone icon indicating copy to clipboard operation
clone copied to clipboard

Not cloning object getters and setters as expected

Open bertolo1988 opened this issue 2 years ago • 2 comments

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'
   )
 })

bertolo1988 avatar Oct 09 '22 23:10 bertolo1988

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.

pvorb avatar Oct 13 '22 07:10 pvorb

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

jimmywarting avatar Sep 26 '23 11:09 jimmywarting