返回一个对象每个属性的完整的属性描述信息
    在ES6后我们可以给对象定义getter和setter属性,这些不能被Object.assign复制;
    这个属性就可以获取到这些属性描述信息,然后可以方便在定义其他对象时复用

    1. // Object.getOwnPropertyDescriptors ----------------------------------------
    2. const p1 = {
    3. firstName: 'Lei',
    4. lastName: 'Wang',
    5. get fullName () {
    6. return this.firstName + ' ' + this.lastName
    7. }
    8. }
    9. // console.log(p1.fullName) // Lei Wang
    10. // const p2 = Object.assign({}, p1)
    11. // p2.firstName = 'zce'
    12. // console.log(p2) // { firstName: 'zce', lastName: 'Wang', fullName: 'Lei Wang' }
    13. const descriptors = Object.getOwnPropertyDescriptors(p1)
    14. console.log(descriptors)
    15. /*
    16. {
    17. firstName: {
    18. value: 'Lei',
    19. writable: true,
    20. enumerable: true,
    21. configurable: true
    22. },
    23. lastName: {
    24. value: 'Wang',
    25. writable: true,
    26. enumerable: true,
    27. configurable: true
    28. },
    29. fullName: {
    30. get: [Function: get fullName],
    31. set: undefined,
    32. enumerable: true,
    33. configurable: true
    34. }
    35. }
    36. */
    37. const p2 = Object.defineProperties({}, descriptors)
    38. p2.firstName = 'zce'
    39. console.log(p2.fullName) // zce Wang