6817d02d8bed08727883862ec3b64f2eb20eb21c628269f97af3c1c5ac1e9e87994558d255c833c22f3a881425e9a72df96c68e4516ef643a66918c8d6077e 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # babel-plugin-transform-es2015-computed-properties
  2. > Compile ES2015 computed properties to ES5
  3. ## Example
  4. **In**
  5. ```js
  6. var obj = {
  7. ["x" + foo]: "heh",
  8. ["y" + bar]: "noo",
  9. foo: "foo",
  10. bar: "bar"
  11. };
  12. ```
  13. **Out**
  14. ```js
  15. var _obj;
  16. function _defineProperty(obj, key, value) {
  17. if (key in obj) {
  18. Object.defineProperty(obj, key, {
  19. value: value,
  20. enumerable: true,
  21. configurable: true,
  22. writable: true
  23. });
  24. } else {
  25. obj[key] = value;
  26. }
  27. return obj;
  28. }
  29. var obj = (
  30. _obj = {},
  31. _defineProperty(_obj, "x" + foo, "heh"),
  32. _defineProperty(_obj, "y" + bar, "noo"),
  33. _defineProperty(_obj, "foo", "foo"),
  34. _defineProperty(_obj, "bar", "bar"),
  35. _obj
  36. );
  37. ```
  38. ## Installation
  39. ```sh
  40. npm install --save-dev babel-plugin-transform-es2015-computed-properties
  41. ```
  42. ## Usage
  43. ### Via `.babelrc` (Recommended)
  44. **.babelrc**
  45. Without options:
  46. ```json
  47. {
  48. "plugins": ["transform-es2015-computed-properties"]
  49. }
  50. ```
  51. With options:
  52. ```json
  53. {
  54. "plugins": [
  55. ["transform-es2015-computed-properties", {
  56. "loose": true
  57. }]
  58. ]
  59. }
  60. ```
  61. ### Via CLI
  62. ```sh
  63. babel --plugins transform-es2015-computed-properties script.js
  64. ```
  65. ### Via Node API
  66. ```javascript
  67. require("babel-core").transform("code", {
  68. plugins: ["transform-es2015-computed-properties"]
  69. });
  70. ```
  71. ## Options
  72. ### `loose`
  73. `boolean`, defaults to `false`
  74. Just like method assignment in classes, in loose mode, computed property names
  75. use simple assignments instead of being defined. This is unlikely to be an issue
  76. in production code.
  77. #### Example
  78. ***In***
  79. ```js
  80. var obj = {
  81. ["x" + foo]: "heh",
  82. ["y" + bar]: "noo",
  83. foo: "foo",
  84. bar: "bar"
  85. };
  86. ```
  87. ***Out***
  88. ```js
  89. var _obj;
  90. var obj = (
  91. _obj = {},
  92. _obj["x" + foo] = "heh",
  93. _obj["y" + bar] = "noo",
  94. _obj.foo = "foo",
  95. _obj.bar = "bar",
  96. _obj
  97. );
  98. ```