Lazy-property

lazy-property

Add a lazily initialized property to an object.

lazy-propert play well with Object.defineProperty to provide laziness over object's properties.

Get it: npm install lazy-property

Sample usage:

1
require("lazy-property")(obj, name, init[, enumerable])

^ Adds a lazily initialized property to the object.

  • obj is the object to add the property to
  • name is the name of the property
  • init is a function that computes the value of the property
  • enumerable if the property is enumerable (default false)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var addLazyProperty = require("lazy-property")

var obj = {}

addLazyProperty(obj, "foo", function() {
  console.log("initialized!")
  return "bar"
})

//Access the property
console.log(obj.foo)
console.log(obj.foo)

//Prints out:
//
//    initialized!
//    bar
//    bar
//

GIF FTW!

lazy-property

Suggest a module

Comments