sensordb2 e64a8defe5 3.17 | hai 1 ano | |
---|---|---|
.. | ||
.github | hai 1 ano | |
test | hai 1 ano | |
.editorconfig | hai 1 ano | |
CHANGELOG.md | hai 1 ano | |
CHANGES | hai 1 ano | |
LICENSE | hai 1 ano | |
README.md | hai 1 ano | |
auto-bind.js | hai 1 ano | |
index.js | hai 1 ano | |
lazy.js | hai 1 ano | |
package.json | hai 1 ano |
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
},
withdraw: {
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 () {
/* ... */
}),
withdraw: 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 }
$ npm install d
To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: Browserify, Webmake or Webpack
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; };
Object.defineProperties(Foo.prototype, autoBind({
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 () {};
Object.defineProperties(Foo.prototype, lazy({ items: d(function () { return []; }) }));
var foo = new Foo();
foo.items.push(1, 2); // foo.items array created and defined directly on foo
$ npm test
To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.