333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# babel-plugin-transform-es2015-classes
 
> Compile ES2015 classes to ES5
 
## Caveats
 
Built-in classes such as `Date`, `Array`, `DOM` etc cannot be properly subclassed
due to limitations in ES5 (for the [es2015-classes](http://babeljs.io/docs/plugins/transform-es2015-classes) plugin).
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.
 
## Installation
 
```sh
npm install --save-dev babel-plugin-transform-es2015-classes
```
 
## Usage
 
### Via `.babelrc` (Recommended)
 
**.babelrc**
 
```js
// without options
{
  "plugins": ["transform-es2015-classes"]
}
 
// with options
{
  "plugins": [
    ["transform-es2015-classes", {
      "loose": true
    }]
  ]
}
```
 
### Via CLI
 
```sh
babel --plugins transform-es2015-classes script.js
```
 
### Via Node API
 
```javascript
require("babel-core").transform("code", {
  plugins: ["transform-es2015-classes"]
});
```
 
## Options
 
### `loose`
 
`boolean`, defaults to `false`.
 
#### Method enumerability
 
Please note that in loose mode class methods **are** enumerable. This is not in line
with the spec and you may run into issues.
 
#### Method assignment
 
Under loose mode, methods are defined on the class prototype with simple assignments
instead of being defined. This can result in the following not working:
 
```javascript
class Foo {
  set bar() {
    throw new Error("foo!");
  }
}
 
class Bar extends Foo {
  bar() {
    // will throw an error when this method is defined
  }
}
```
 
When `Bar.prototype.foo` is defined it triggers the setter on `Foo`. This is a
case that is very unlikely to appear in production code however it's something
to keep in mind.