1 |
3a515b92
|
cagy
|
# prr [](http://travis-ci.org/rvagg/prr)
|
2 |
|
|
|
3 |
|
|
An sensible alternative to `Object.defineProperty()`. Available in npm and Ender as **prr**.
|
4 |
|
|
|
5 |
|
|
## Usage
|
6 |
|
|
|
7 |
|
|
Set the property `'foo'` (`obj.foo`) to have the value `'bar'` with default options (`'enumerable'`, `'configurable'` and `'writable'` are all `false`):
|
8 |
|
|
|
9 |
|
|
```js
|
10 |
|
|
prr(obj, 'foo', 'bar')
|
11 |
|
|
```
|
12 |
|
|
|
13 |
|
|
Adjust the default options:
|
14 |
|
|
|
15 |
|
|
```js
|
16 |
|
|
prr(obj, 'foo', 'bar', { enumerable: true, writable: true })
|
17 |
|
|
```
|
18 |
|
|
|
19 |
|
|
Do the same operation for multiple properties:
|
20 |
|
|
|
21 |
|
|
```js
|
22 |
|
|
prr(obj, { one: 'one', two: 'two' })
|
23 |
|
|
// or with options:
|
24 |
|
|
prr(obj, { one: 'one', two: 'two' }, { enumerable: true, writable: true })
|
25 |
|
|
```
|
26 |
|
|
|
27 |
|
|
### Simplify!
|
28 |
|
|
|
29 |
|
|
But obviously, having to write out the full options object makes it nearly as bad as the original `Object.defineProperty()` so we can simplify.
|
30 |
|
|
|
31 |
|
|
As an alternative method we can use an options string where each character represents a option: `'e'=='enumerable'`, `'c'=='configurable'` and `'w'=='writable'`:
|
32 |
|
|
|
33 |
|
|
```js
|
34 |
|
|
prr(obj, 'foo', 'bar', 'ew') // enumerable and writable but not configurable
|
35 |
|
|
// muliple properties:
|
36 |
|
|
prr(obj, { one: 'one', two: 'two' }, 'ewc') // configurable too
|
37 |
|
|
```
|
38 |
|
|
|
39 |
|
|
## Where can I use it?
|
40 |
|
|
|
41 |
|
|
Anywhere! For pre-ES5 environments *prr* will simply fall-back to an `object[property] = value` so you can get close to what you want.
|
42 |
|
|
|
43 |
|
|
*prr* is Ender-compatible so you can include it in your Ender build and `$.prr(...)` or `var prr = require('prr'); prr(...)`.
|
44 |
|
|
|
45 |
|
|
## Licence
|
46 |
|
|
|
47 |
|
|
prr is Copyright (c) 2013 Rod Vagg [@rvagg](https://twitter.com/rvagg) and licensed under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE.md file for more details.
|