此代码适用于http://jsbin.com(ES6 / Babel)
class Human {
gender = 'male';
prnG = ()=> {
console.log(this.gender)
}
}
class Person extends Human {
name = 'Ana';
gender = 'female';
prnN = ()=> {
console.log(this.name);
}
}
const hum = new Human();
hum.prnG(); // "male"
const person = new Person();
person.prnG(); // "female"
person.prnN(); // "Ana"
如何在WebStorm中使用没有构造函数的类使用Babel设置在ES6中使用constuctor和property进行转换(this.gender =' male')?
答案 0 :(得分:1)
你需要transform-class-properties Babel插件。
npm i babel-plugin-transform-class-properties babel-preset-env --save-dev
:
{ “插件”:[ “变换级的属性” ] “预设”:[ “ENV” ] }