You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.6 KiB

4 years ago
  1. #object.getownpropertydescriptors <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
  2. [![Build Status][travis-svg]][travis-url]
  3. [![dependency status][deps-svg]][deps-url]
  4. [![dev dependency status][dev-deps-svg]][dev-deps-url]
  5. [![License][license-image]][license-url]
  6. [![Downloads][downloads-image]][downloads-url]
  7. [![npm badge][npm-badge-png]][package-url]
  8. [![browser support][testling-svg]][testling-url]
  9. An ES2017 spec-compliant shim for `Object.getOwnPropertyDescriptors` that works in ES5.
  10. Invoke its "shim" method to shim `Object.getOwnPropertyDescriptors` if it is unavailable, and if `Object.getOwnPropertyDescriptor` is available.
  11. This package implements the [es-shim API](https://github.com/es-shims/api) interface. It works in an ES3-supported environment and complies with the [spec](https://github.com/tc39/ecma262/pull/582).
  12. ## Example
  13. ```js
  14. var getDescriptors = require('object.getownpropertydescriptors');
  15. var assert = require('assert');
  16. var obj = { normal: Infinity };
  17. var enumDescriptor = {
  18. enumerable: false,
  19. writable: false,
  20. configurable: true,
  21. value: true
  22. };
  23. var writableDescriptor = {
  24. enumerable: true,
  25. writable: true,
  26. configurable: true,
  27. value: 42
  28. };
  29. var symbol = Symbol();
  30. var symDescriptor = {
  31. enumerable: true,
  32. writable: true,
  33. configurable: false,
  34. value: [symbol]
  35. };
  36. Object.defineProperty(obj, 'enumerable', enumDescriptor);
  37. Object.defineProperty(obj, 'writable', writableDescriptor);
  38. Object.defineProperty(obj, 'symbol', symDescriptor);
  39. var descriptors = getDescriptors(obj);
  40. assert.deepEqual(descriptors, {
  41. normal: {
  42. enumerable: true,
  43. writable: true,
  44. configurable: true,
  45. value: Infinity
  46. },
  47. enumerable: enumDescriptor,
  48. writable: writableDescriptor,
  49. symbol: symDescriptor
  50. });
  51. ```
  52. ```js
  53. var getDescriptors = require('object.getownpropertydescriptors');
  54. var assert = require('assert');
  55. /* when Object.getOwnPropertyDescriptors is not present */
  56. delete Object.getOwnPropertyDescriptors;
  57. var shimmedDescriptors = getDescriptors.shim();
  58. assert.equal(shimmedDescriptors, getDescriptors);
  59. assert.deepEqual(shimmedDescriptors(obj), getDescriptors(obj));
  60. ```
  61. ```js
  62. var getDescriptors = require('object.getownpropertydescriptors');
  63. var assert = require('assert');
  64. /* when Object.getOwnPropertyDescriptors is present */
  65. var shimmedDescriptors = getDescriptors.shim();
  66. assert.notEqual(shimmedDescriptors, getDescriptors);
  67. assert.deepEqual(shimmedDescriptors(obj), getDescriptors(obj));
  68. ```
  69. ## Tests
  70. Simply clone the repo, `npm install`, and run `npm test`
  71. [package-url]: https://npmjs.org/package/object.getownpropertydescriptors
  72. [npm-version-svg]: http://versionbadg.es/ljharb/object.getownpropertydescriptors.svg
  73. [travis-svg]: https://travis-ci.org/ljharb/object.getownpropertydescriptors.svg
  74. [travis-url]: https://travis-ci.org/ljharb/object.getownpropertydescriptors
  75. [deps-svg]: https://david-dm.org/ljharb/object.getownpropertydescriptors.svg
  76. [deps-url]: https://david-dm.org/ljharb/object.getownpropertydescriptors
  77. [dev-deps-svg]: https://david-dm.org/ljharb/object.getownpropertydescriptors/dev-status.svg
  78. [dev-deps-url]: https://david-dm.org/ljharb/object.getownpropertydescriptors#info=devDependencies
  79. [testling-svg]: https://ci.testling.com/ljharb/object.getownpropertydescriptors.png
  80. [testling-url]: https://ci.testling.com/ljharb/object.getownpropertydescriptors
  81. [npm-badge-png]: https://nodei.co/npm/object.getownpropertydescriptors.png?downloads=true&stars=true
  82. [license-image]: http://img.shields.io/npm/l/object.getownpropertydescriptors.svg
  83. [license-url]: LICENSE
  84. [downloads-image]: http://img.shields.io/npm/dm/object.getownpropertydescriptors.svg
  85. [downloads-url]: http://npm-stat.com/charts.html?package=object.getownpropertydescriptors