classPerson{
constructor(name) {
this.name = name;
}
sayHi() {
return`Hi, this is ${this.name}`;
}
}
.babelrc 配置为标准模式时:
{
"presets": [
"es2015"
]
}
转换以后的代码为:
"use strict";
var _createClass = function () { functiondefineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value"in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } returnfunction (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function_classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { thrownewTypeError("Cannot call a class as a function"); } }
var Person = function () {
functionPerson(name) {
_classCallCheck(this, Person);
this.name = name;
}
_createClass(Person, [{
key: "sayHi",
value: functionsayHi() {
return"Hi, this is " + this.name;
}
}]);
return Person;
}();
使用 loose preset 后:
{
"presets": [
"es2015-loose"
]
}
转换结果为:
"use strict";
function_classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { thrownewTypeError("Cannot call a class as a function"); } }
var Person = function () {
functionPerson(name) {
_classCallCheck(this, Person);
this.name = name;
}
Person.prototype.sayHi = functionsayHi() {
return"Hi, this is " + this.name;
};
return Person;
}();