clone
clone copied to clipboard
Not cloning object getters and setters as expected
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'
)
})
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.
could also be worth mentioning that there also is a built in structuredClone()
that do also support getters.