Object.getOwnPropertyDescriptors

This method returns all properties including getter and setter.

Object.assign shallow copies all the properties excluding getter and setter of the original source object.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const car = {
name: 'BMW',
price: 100000,
get discount () {
return this.d
}
set discount (x) {
this.d = x
}
}

Object.getOwnPropertyDescriptor(car, 'discount') // {get, set, enumerable, configurable}

const assignedCar = Object.assign({}, car)
Object.getOwnPropertyDescriptor(assignedCar, 'discount') // {value: undefined}

const definedCar = Object.defineProperties({}, Object.getOwnPropertyDescriptors(car))
Object.getOwnPropertyDescriptor(definedCar, 'discount') // {get, set, enumerable, configurable}