ed153182ed3bf21586326deff7d57fe5c934acf5886aa3ad74ce50b7f2af56b699c8f0b8e07d1323376ac8a6e8b99d91f140bceb55f92bebd9cd6e847c4d2c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # babel-plugin-transform-es2015-classes
  2. > Compile ES2015 classes to ES5
  3. ## Caveats
  4. Built-in classes such as `Date`, `Array`, `DOM` etc cannot be properly subclassed
  5. due to limitations in ES5 (for the [es2015-classes](http://babeljs.io/docs/plugins/transform-es2015-classes) plugin).
  6. You can try to use [babel-plugin-transform-builtin-extend](https://github.com/loganfsmyth/babel-plugin-transform-builtin-extend) based on `Object.setPrototypeOf` and `Reflect.construct`, but it also has some limitations.
  7. ## Installation
  8. ```sh
  9. npm install --save-dev babel-plugin-transform-es2015-classes
  10. ```
  11. ## Usage
  12. ### Via `.babelrc` (Recommended)
  13. **.babelrc**
  14. ```js
  15. // without options
  16. {
  17. "plugins": ["transform-es2015-classes"]
  18. }
  19. // with options
  20. {
  21. "plugins": [
  22. ["transform-es2015-classes", {
  23. "loose": true
  24. }]
  25. ]
  26. }
  27. ```
  28. ### Via CLI
  29. ```sh
  30. babel --plugins transform-es2015-classes script.js
  31. ```
  32. ### Via Node API
  33. ```javascript
  34. require("babel-core").transform("code", {
  35. plugins: ["transform-es2015-classes"]
  36. });
  37. ```
  38. ## Options
  39. ### `loose`
  40. `boolean`, defaults to `false`.
  41. #### Method enumerability
  42. Please note that in loose mode class methods **are** enumerable. This is not in line
  43. with the spec and you may run into issues.
  44. #### Method assignment
  45. Under loose mode, methods are defined on the class prototype with simple assignments
  46. instead of being defined. This can result in the following not working:
  47. ```javascript
  48. class Foo {
  49. set bar() {
  50. throw new Error("foo!");
  51. }
  52. }
  53. class Bar extends Foo {
  54. bar() {
  55. // will throw an error when this method is defined
  56. }
  57. }
  58. ```
  59. When `Bar.prototype.foo` is defined it triggers the setter on `Foo`. This is a
  60. case that is very unlikely to appear in production code however it's something
  61. to keep in mind.