Last change
on this file since b738035 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.3 KB
|
Rev | Line | |
---|
[6a3a178] | 1 | # define-lazy-prop [![Build Status](https://travis-ci.org/sindresorhus/define-lazy-prop.svg?branch=master)](https://travis-ci.org/sindresorhus/define-lazy-prop)
|
---|
| 2 |
|
---|
| 3 | > Define a [lazily evaluated](https://en.wikipedia.org/wiki/Lazy_evaluation) property on an object
|
---|
| 4 |
|
---|
| 5 | Useful when the value of a property is expensive to generate, so you want to delay the computation until the property is needed. For example, improving startup performance by deferring nonessential operations.
|
---|
| 6 |
|
---|
| 7 |
|
---|
| 8 | ## Install
|
---|
| 9 |
|
---|
| 10 | ```
|
---|
| 11 | $ npm install define-lazy-prop
|
---|
| 12 | ```
|
---|
| 13 |
|
---|
| 14 |
|
---|
| 15 | ## Usage
|
---|
| 16 |
|
---|
| 17 | ```js
|
---|
| 18 | const defineLazyProp = require('define-lazy-prop');
|
---|
| 19 |
|
---|
| 20 | const unicorn = {
|
---|
| 21 | // …
|
---|
| 22 | };
|
---|
| 23 |
|
---|
| 24 | defineLazyProp(unicorn, 'rainbow', () => expensiveComputation());
|
---|
| 25 |
|
---|
| 26 | app.on('user-action', () => {
|
---|
| 27 | doSomething(unicorn.rainbow);
|
---|
| 28 | });
|
---|
| 29 | ```
|
---|
| 30 |
|
---|
| 31 |
|
---|
| 32 | ## API
|
---|
| 33 |
|
---|
| 34 | ### defineLazyProp(object, propertyName, fn)
|
---|
| 35 |
|
---|
| 36 | #### object
|
---|
| 37 |
|
---|
| 38 | Type: `Object`
|
---|
| 39 |
|
---|
| 40 | Object to add property to.
|
---|
| 41 |
|
---|
| 42 | #### propertyName
|
---|
| 43 |
|
---|
| 44 | Type: `string`
|
---|
| 45 |
|
---|
| 46 | Name of the property to add.
|
---|
| 47 |
|
---|
| 48 | #### fn
|
---|
| 49 |
|
---|
| 50 | Type: `Function`
|
---|
| 51 |
|
---|
| 52 | Called the first time `propertyName` is accessed. Expected to return a value.
|
---|
| 53 |
|
---|
| 54 |
|
---|
| 55 | ## Related
|
---|
| 56 |
|
---|
| 57 | - [lazy-value](https://github.com/sindresorhus/lazy-value) - Create a lazily evaluated value
|
---|
| 58 | - [import-lazy](https://github.com/sindresorhus/import-lazy) - Import a module lazily
|
---|
| 59 | - [p-lazy](https://github.com/sindresorhus/p-lazy) - Create a lazy promise
|
---|
| 60 |
|
---|
| 61 |
|
---|
| 62 | ## License
|
---|
| 63 |
|
---|
| 64 | MIT © [Sindre Sorhus](https://sindresorhus.com)
|
---|
Note:
See
TracBrowser
for help on using the repository browser.