@babel/plugin-transform-class-properties
信息
This plugin is included in @babel/preset-env
, in ES2022
Example
Below is a class with four class properties which will be transformed.
JavaScript
class Bork {
//Property initializer syntax
instanceProperty = "bork";
boundFunction = () => {
return this.instanceProperty;
};
//Static class properties
static staticProperty = "babelIsCool";
static staticFunction = function() {
return Bork.staticProperty;
};
}
let myBork = new Bork();
//Property initializers are not on the prototype.
console.log(myBork.__proto__.boundFunction); // > undefined
//Bound functions are bound to the class instance.
console.log(myBork.boundFunction.call(undefined)); // > "bork"
//Static function exists on the class.
console.log(Bork.staticFunction()); // > "babelIsCool"
Installation
- npm
- Yarn
- pnpm
npm install --save-dev @babel/plugin-transform-class-properties
yarn add --dev @babel/plugin-transform-class-properties
pnpm add --save-dev @babel/plugin-transform-class-properties
Usage
With a configuration file (Recommended)
Without options:
babel.config.json
{
"plugins": ["@babel/plugin-transform-class-properties"]
}
With options:
babel.config.json
{
"plugins": [["@babel/plugin-transform-class-properties", { "loose": true }]]
}
Via CLI
Shell
babel --plugins @babel/plugin-transform-class-properties script.js