1 | # define-data-property <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
|
---|
2 |
|
---|
3 | [![github actions][actions-image]][actions-url]
|
---|
4 | [![coverage][codecov-image]][codecov-url]
|
---|
5 | [![License][license-image]][license-url]
|
---|
6 | [![Downloads][downloads-image]][downloads-url]
|
---|
7 |
|
---|
8 | [![npm badge][npm-badge-png]][package-url]
|
---|
9 |
|
---|
10 | Define a data property on an object. Will fall back to assignment in an engine without descriptors.
|
---|
11 |
|
---|
12 | The three `non*` argument can also be passed `null`, which will use the existing state if available.
|
---|
13 |
|
---|
14 | The `loose` argument will mean that if you attempt to set a non-normal data property, in an environment without descriptor support, it will fall back to normal assignment.
|
---|
15 |
|
---|
16 | ## Usage
|
---|
17 |
|
---|
18 | ```javascript
|
---|
19 | var defineDataProperty = require('define-data-property');
|
---|
20 | var assert = require('assert');
|
---|
21 |
|
---|
22 | var obj = {};
|
---|
23 | defineDataProperty(obj, 'key', 'value');
|
---|
24 | defineDataProperty(
|
---|
25 | obj,
|
---|
26 | 'key2',
|
---|
27 | 'value',
|
---|
28 | true, // nonEnumerable, optional
|
---|
29 | false, // nonWritable, optional
|
---|
30 | true, // nonConfigurable, optional
|
---|
31 | false // loose, optional
|
---|
32 | );
|
---|
33 |
|
---|
34 | assert.deepEqual(
|
---|
35 | Object.getOwnPropertyDescriptors(obj),
|
---|
36 | {
|
---|
37 | key: {
|
---|
38 | configurable: true,
|
---|
39 | enumerable: true,
|
---|
40 | value: 'value',
|
---|
41 | writable: true,
|
---|
42 | },
|
---|
43 | key2: {
|
---|
44 | configurable: false,
|
---|
45 | enumerable: false,
|
---|
46 | value: 'value',
|
---|
47 | writable: true,
|
---|
48 | },
|
---|
49 | }
|
---|
50 | );
|
---|
51 | ```
|
---|
52 |
|
---|
53 | [package-url]: https://npmjs.org/package/define-data-property
|
---|
54 | [npm-version-svg]: https://versionbadg.es/ljharb/define-data-property.svg
|
---|
55 | [deps-svg]: https://david-dm.org/ljharb/define-data-property.svg
|
---|
56 | [deps-url]: https://david-dm.org/ljharb/define-data-property
|
---|
57 | [dev-deps-svg]: https://david-dm.org/ljharb/define-data-property/dev-status.svg
|
---|
58 | [dev-deps-url]: https://david-dm.org/ljharb/define-data-property#info=devDependencies
|
---|
59 | [npm-badge-png]: https://nodei.co/npm/define-data-property.png?downloads=true&stars=true
|
---|
60 | [license-image]: https://img.shields.io/npm/l/define-data-property.svg
|
---|
61 | [license-url]: LICENSE
|
---|
62 | [downloads-image]: https://img.shields.io/npm/dm/define-data-property.svg
|
---|
63 | [downloads-url]: https://npm-stat.com/charts.html?package=define-data-property
|
---|
64 | [codecov-image]: https://codecov.io/gh/ljharb/define-data-property/branch/main/graphs/badge.svg
|
---|
65 | [codecov-url]: https://app.codecov.io/gh/ljharb/define-data-property/
|
---|
66 | [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/ljharb/define-data-property
|
---|
67 | [actions-url]: https://github.com/ljharb/define-data-property/actions
|
---|