https://babel.docschina.org/docs/en/babel-plugin-proposal-class-properties
yarn add -D @babel/plugin-proposal-class-properties
配置参考
{
"plugins": ["@babel/plugin-proposal-class-properties"]
}
支持对以下4中语法的转换
class Bork {
// 属性初始化器语法
instanceProperty = "bork";
boundFunction = () => {
return this.instanceProperty;
};
// 静态类属性
static staticProperty = "babelIsCool";
static staticFunction = function() {
return Bork.staticProperty;
};
}
let myBork = new Bork;
// 属性初始化器设定的值不在原型上
console.log(myBork.__proto__.boundFunction); // > undefined
// 绑定函数绑定到类实例上
console.log(myBork.boundFunction.call(undefined)); // > "bork"
// 类上包含静态函数
console.log(Bork.staticFunction()); // > "babelIsCool"
网友评论