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.

86 lines
2.7 KiB

4 years ago
  1. #define-properties <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. Define multiple non-enumerable properties at once. Uses `Object.defineProperty` when available; falls back to standard assignment in older engines.
  10. Existing properties are not overridden. Accepts a map of property names to a predicate that, when true, force-overrides.
  11. ## Example
  12. ```js
  13. var define = require('define-properties');
  14. var assert = require('assert');
  15. var obj = define({ a: 1, b: 2 }, {
  16. a: 10,
  17. b: 20,
  18. c: 30
  19. });
  20. assert(obj.a === 1);
  21. assert(obj.b === 2);
  22. assert(obj.c === 30);
  23. if (define.supportsDescriptors) {
  24. assert.deepEqual(Object.keys(obj), ['a', 'b']);
  25. assert.deepEqual(Object.getOwnPropertyDescriptor(obj, 'c'), {
  26. configurable: true,
  27. enumerable: false,
  28. value: 30,
  29. writable: false
  30. });
  31. }
  32. ```
  33. Then, with predicates:
  34. ```js
  35. var define = require('define-properties');
  36. var assert = require('assert');
  37. var obj = define({ a: 1, b: 2, c: 3 }, {
  38. a: 10,
  39. b: 20,
  40. c: 30
  41. }, {
  42. a: function () { return false; },
  43. b: function () { return true; }
  44. });
  45. assert(obj.a === 1);
  46. assert(obj.b === 20);
  47. assert(obj.c === 3);
  48. if (define.supportsDescriptors) {
  49. assert.deepEqual(Object.keys(obj), ['a', 'c']);
  50. assert.deepEqual(Object.getOwnPropertyDescriptor(obj, 'b'), {
  51. configurable: true,
  52. enumerable: false,
  53. value: 20,
  54. writable: false
  55. });
  56. }
  57. ```
  58. ## Tests
  59. Simply clone the repo, `npm install`, and run `npm test`
  60. [package-url]: https://npmjs.org/package/define-properties
  61. [npm-version-svg]: http://versionbadg.es/ljharb/define-properties.svg
  62. [travis-svg]: https://travis-ci.org/ljharb/define-properties.svg
  63. [travis-url]: https://travis-ci.org/ljharb/define-properties
  64. [deps-svg]: https://david-dm.org/ljharb/define-properties.svg
  65. [deps-url]: https://david-dm.org/ljharb/define-properties
  66. [dev-deps-svg]: https://david-dm.org/ljharb/define-properties/dev-status.svg
  67. [dev-deps-url]: https://david-dm.org/ljharb/define-properties#info=devDependencies
  68. [testling-svg]: https://ci.testling.com/ljharb/define-properties.png
  69. [testling-url]: https://ci.testling.com/ljharb/define-properties
  70. [npm-badge-png]: https://nodei.co/npm/define-properties.png?downloads=true&stars=true
  71. [license-image]: http://img.shields.io/npm/l/define-properties.svg
  72. [license-url]: LICENSE
  73. [downloads-image]: http://img.shields.io/npm/dm/define-properties.svg
  74. [downloads-url]: http://npm-stat.com/charts.html?package=define-properties