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.

47 lines
1.7 KiB

4 years ago
  1. # prr [![Build Status](https://secure.travis-ci.org/rvagg/prr.png)](http://travis-ci.org/rvagg/prr)
  2. An sensible alternative to `Object.defineProperty()`. Available in npm and Ender as **prr**.
  3. ## Usage
  4. Set the property `'foo'` (`obj.foo`) to have the value `'bar'` with default options (`'enumerable'`, `'configurable'` and `'writable'` are all `false`):
  5. ```js
  6. prr(obj, 'foo', 'bar')
  7. ```
  8. Adjust the default options:
  9. ```js
  10. prr(obj, 'foo', 'bar', { enumerable: true, writable: true })
  11. ```
  12. Do the same operation for multiple properties:
  13. ```js
  14. prr(obj, { one: 'one', two: 'two' })
  15. // or with options:
  16. prr(obj, { one: 'one', two: 'two' }, { enumerable: true, writable: true })
  17. ```
  18. ### Simplify!
  19. But obviously, having to write out the full options object makes it nearly as bad as the original `Object.defineProperty()` so we can simplify.
  20. As an alternative method we can use an options string where each character represents a option: `'e'=='enumerable'`, `'c'=='configurable'` and `'w'=='writable'`:
  21. ```js
  22. prr(obj, 'foo', 'bar', 'ew') // enumerable and writable but not configurable
  23. // muliple properties:
  24. prr(obj, { one: 'one', two: 'two' }, 'ewc') // configurable too
  25. ```
  26. ## Where can I use it?
  27. Anywhere! For pre-ES5 environments *prr* will simply fall-back to an `object[property] = value` so you can get close to what you want.
  28. *prr* is Ender-compatible so you can include it in your Ender build and `$.prr(...)` or `var prr = require('prr'); prr(...)`.
  29. ## Licence
  30. 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.