]> git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/vim-mode/node_modules/grim/node_modules/emissary/node_modules/es6-weak-map/node_modules/d/README.md
872d493ed86b6b91daf44176b9a873f2dd78ff09
[rbdr/dotfiles] / atom / packages / vim-mode / node_modules / grim / node_modules / emissary / node_modules / es6-weak-map / node_modules / d / README.md
1 # D - Property descriptor factory
2
3 _Originally derived from [es5-ext](https://github.com/medikoo/es5-ext) package._
4
5 Defining properties with descriptors is very verbose:
6
7 ```javascript
8 var Account = function () {};
9 Object.defineProperties(Account.prototype, {
10 deposit: { value: function () {
11 /* ... */
12 }, configurable: true, enumerable: false, writable: true },
13 whithdraw: { value: function () {
14 /* ... */
15 }, configurable: true, enumerable: false, writable: true },
16 balance: { get: function () {
17 /* ... */
18 }, configurable: true, enumerable: false }
19 });
20 ```
21
22 D cuts that to:
23
24 ```javascript
25 var d = require('d');
26
27 var Account = function () {};
28 Object.defineProperties(Account.prototype, {
29 deposit: d(function () {
30 /* ... */
31 }),
32 whithdraw: d(function () {
33 /* ... */
34 }),
35 balance: d.gs(function () {
36 /* ... */
37 })
38 });
39 ```
40
41 By default, created descriptor follow characteristics of native ES5 properties, and defines values as:
42
43 ```javascript
44 { configurable: true, enumerable: false, writable: true }
45 ```
46
47 You can overwrite it by preceding _value_ argument with instruction:
48 ```javascript
49 d('c', value); // { configurable: true, enumerable: false, writable: false }
50 d('ce', value); // { configurable: true, enumerable: true, writable: false }
51 d('e', value); // { configurable: false, enumerable: true, writable: false }
52
53 // Same way for get/set:
54 d.gs('e', value); // { configurable: false, enumerable: true }
55 ```
56
57 ### Other utilities
58
59 #### autoBind(obj, props) _(d/auto-bind)_
60
61 Define methods which will be automatically bound to its instances
62
63 ```javascript
64 var d = require('d');
65 var autoBind = require('d/auto-bind');
66
67 var Foo = function () { this._count = 0; };
68 autoBind(Foo.prototype, {
69 increment: d(function () { ++this._count; });
70 });
71
72 var foo = new Foo();
73
74 // Increment foo counter on each domEl click
75 domEl.addEventListener('click', foo.increment, false);
76 ```
77
78 #### lazy(obj, props) _(d/lazy)_
79
80 Define lazy properties, which will be resolved on first access
81
82 ```javascript
83 var d = require('d');
84 var lazy = require('d/lazy');
85
86 var Foo = function () {};
87 lazy(Foo.prototype, {
88 items: d(function () { return []; })
89 });
90
91 var foo = new Foo();
92 foo.items.push(1, 2); // foo.items array created
93 ```
94
95 ## Installation
96 ### NPM
97
98 In your project path:
99
100 $ npm install d
101
102 ### Browser
103
104 You can easily bundle _D_ for browser with [modules-webmake](https://github.com/medikoo/modules-webmake)
105
106 ## Tests [![Build Status](https://travis-ci.org/medikoo/d.png)](https://travis-ci.org/medikoo/d)
107
108 $ npm test