web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.3 KiB

  1. # D
  2. ## Property descriptor factory
  3. _Originally derived from [es5-ext](https://github.com/medikoo/es5-ext) package._
  4. Defining properties with descriptors is very verbose:
  5. ```javascript
  6. var Account = function () {};
  7. Object.defineProperties(Account.prototype, {
  8. deposit: {
  9. value: function () {
  10. /* ... */
  11. },
  12. configurable: true,
  13. enumerable: false,
  14. writable: true
  15. },
  16. withdraw: {
  17. value: function () {
  18. /* ... */
  19. },
  20. configurable: true,
  21. enumerable: false,
  22. writable: true
  23. },
  24. balance: {
  25. get: function () {
  26. /* ... */
  27. },
  28. configurable: true,
  29. enumerable: false
  30. }
  31. });
  32. ```
  33. D cuts that to:
  34. ```javascript
  35. var d = require("d");
  36. var Account = function () {};
  37. Object.defineProperties(Account.prototype, {
  38. deposit: d(function () {
  39. /* ... */
  40. }),
  41. withdraw: d(function () {
  42. /* ... */
  43. }),
  44. balance: d.gs(function () {
  45. /* ... */
  46. })
  47. });
  48. ```
  49. By default, created descriptor follow characteristics of native ES5 properties, and defines values as:
  50. ```javascript
  51. { configurable: true, enumerable: false, writable: true }
  52. ```
  53. You can overwrite it by preceding _value_ argument with instruction:
  54. ```javascript
  55. d("c", value); // { configurable: true, enumerable: false, writable: false }
  56. d("ce", value); // { configurable: true, enumerable: true, writable: false }
  57. d("e", value); // { configurable: false, enumerable: true, writable: false }
  58. // Same way for get/set:
  59. d.gs("e", value); // { configurable: false, enumerable: true }
  60. ```
  61. ### Installation
  62. $ npm install d
  63. To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: [Browserify](http://browserify.org/), [Webmake](https://github.com/medikoo/modules-webmake) or [Webpack](http://webpack.github.io/)
  64. ### Other utilities
  65. #### autoBind(obj, props) _(d/auto-bind)_
  66. Define methods which will be automatically bound to its instances
  67. ```javascript
  68. var d = require('d');
  69. var autoBind = require('d/auto-bind');
  70. var Foo = function () { this._count = 0; };
  71. Object.defineProperties(Foo.prototype, autoBind({
  72. increment: d(function () { ++this._count; });
  73. }));
  74. var foo = new Foo();
  75. // Increment foo counter on each domEl click
  76. domEl.addEventListener('click', foo.increment, false);
  77. ```
  78. #### lazy(obj, props) _(d/lazy)_
  79. Define lazy properties, which will be resolved on first access
  80. ```javascript
  81. var d = require("d");
  82. var lazy = require("d/lazy");
  83. var Foo = function () {};
  84. Object.defineProperties(Foo.prototype, lazy({ items: d(function () { return []; }) }));
  85. var foo = new Foo();
  86. foo.items.push(1, 2); // foo.items array created and defined directly on foo
  87. ```
  88. ## Tests [![Build Status](https://travis-ci.org/medikoo/d.png)](https://travis-ci.org/medikoo/d)
  89. $ npm test
  90. ## Security contact information
  91. To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.
  92. ---
  93. <div align="center">
  94. <b>
  95. <a href="https://tidelift.com/subscription/pkg/npm-d?utm_source=npm-d&utm_medium=referral&utm_campaign=readme">Get professional support for d with a Tidelift subscription</a>
  96. </b>
  97. <br>
  98. <sub>
  99. Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
  100. </sub>
  101. </div>