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