# lazyval

> A simple way to define lazy properties to a generic object or a prototype

Latest version **0.2.0** (published 2014-11-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install lazyval
pnpm add lazyval
yarn add lazyval
bun add lazyval
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2014-11-02 |
| First published | 2014-09-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Andrea Giammarchi |
| Maintainers | webreflection |
| Keywords | lazy, property, inheritance, prototype |

## Links

- npm: https://www.npmjs.com/package/lazyval
- Repository: https://github.com/WebReflection/lazyval
- Issues: https://github.com/WebReflection/lazyval/issues
- npm.io page: https://npm.io/package/lazyval

## Dependencies (1)

- [wru](https://npm.io/package/wru.md) >= 0.0.0

## Recent versions

- 0.2.0 (latest) — 2014-11-02
- 0.1.4 — 2014-10-12
- 0.1.1 — 2014-09-26
- 0.1.0 — 2014-09-25

## README

lazyval
=======

[![build status](https://secure.travis-ci.org/WebReflection/lazyval.png)](http://travis-ci.org/WebReflection/lazyval)

A simple way to define lazy properties to a generic object or a prototype.

Compatible with mostly every browser or engine out there: [feel free to test it](http://webreflection.github.io/lazyval/test/).


### function signature

```js
lazyval({}, 'key', function () { return value; });
// returns the generic Object
lazyval(
  // where to define the lazy property
  // can be object or a prototype
  generic:Object,
  // the property name
  property:String,
  // the callback to invoke once
  // the generic object or any inherited one
  // will access this property
  callback:Function
  // this callback will be invoked inside the getter as:
  //  callback.call(this, property, generic);
  // and its returned value will be assigned
);


lazyval({}, {
  key1: function () { return value1; },
  key2: function () { return value2; }
});
// for multiple lazy properties at once
lazyval(
  // where to define the lazy property
  // can be object or a prototype
  generic:Object,
  // a key/value pair where value
  // will be function to lazily invoke
  // once accessed through properties
  properties:Object
);
```


### lazy examples via object and properties

```js
var o = lazyval({}, {
  uid: function () {
    return 'object:' + Math.round(
      Math.random() * Date.now()
    );
  }
});

// only once needed ...
o.uid; // object:27364279
```


### lazy examples via inheritance and 3 arguments

```js
function A() {}

// note lazyval.proto
lazyval.proto(
  A.prototype,
  'expensive',
  function () {
    // do expensive computation here
    // as example ...
    return Math.random();
    // return the value to lazily assign
    // as property name of the object
  }
);

var
  a1 = new A,
  a2 = new A
;

a1.expensive; // 0.346731865
a1.expensive === a1.expensive; // true
a2.expensive === a1.expensive; // false
a2.expensive; // 0.874216789

// clean up
delete a1.expensive;
a1.expensive; // 0.4567891
a1.expensive === a1.expensive; // true
```

#### lazyval.direct VS lazyval.proto
The difference is that `lazyval.proto` will not actually replace at runtime the property to the prototype object itself but only to inheriting isntances. `lazyval.direct` is somehow more greedy here because an access to the prototype object could affect all inherited instances.

For classes and prototypal inheritance, `lazyval.proto` is recommended.

---
_Source: https://npm.io/package/lazyval · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
