Bachir Soussi Chiadmi 0611418f7a added whole system from ola4doc | 7 years ago | |
---|---|---|
.. | ||
test | 7 years ago | |
.lint | 7 years ago | |
.npmignore | 7 years ago | |
.travis.yml | 7 years ago | |
CHANGES | 7 years ago | |
LICENCE | 7 years ago | |
README.md | 7 years ago | |
auto-bind.js | 7 years ago | |
index.js | 7 years ago | |
lazy.js | 7 years ago | |
package.json | 7 years ago |
Originally derived from es5-ext package.
Defining properties with descriptors is very verbose:
var Account = function () {};
Object.defineProperties(Account.prototype, {
deposit: { value: function () {
/* ... */
}, configurable: true, enumerable: false, writable: true },
whithdraw: { value: function () {
/* ... */
}, configurable: true, enumerable: false, writable: true },
balance: { get: function () {
/* ... */
}, configurable: true, enumerable: false }
});
D cuts that to:
var d = require('d');
var Account = function () {};
Object.defineProperties(Account.prototype, {
deposit: d(function () {
/* ... */
}),
whithdraw: d(function () {
/* ... */
}),
balance: d.gs(function () {
/* ... */
})
});
By default, created descriptor follow characteristics of native ES5 properties, and defines values as:
{ configurable: true, enumerable: false, writable: true }
You can overwrite it by preceding value argument with instruction:
d('c', value); // { configurable: true, enumerable: false, writable: false }
d('ce', value); // { configurable: true, enumerable: true, writable: false }
d('e', value); // { configurable: false, enumerable: true, writable: false }
// Same way for get/set:
d.gs('e', value); // { configurable: false, enumerable: true }
Define methods which will be automatically bound to its instances
var d = require('d');
var autoBind = require('d/auto-bind');
var Foo = function () { this._count = 0; };
autoBind(Foo.prototype, {
increment: d(function () { ++this._count; });
});
var foo = new Foo();
// Increment foo counter on each domEl click
domEl.addEventListener('click', foo.increment, false);
Define lazy properties, which will be resolved on first access
var d = require('d');
var lazy = require('d/lazy');
var Foo = function () {};
lazy(Foo.prototype, {
items: d(function () { return []; })
});
var foo = new Foo();
foo.items.push(1, 2); // foo.items array created
In your project path:
$ npm install d
You can easily bundle D for browser with modules-webmake
$ npm test