大家好,又见面了,我是你们的朋友全栈君。
步骤
初始化环境
代码语言:javascript复制npm init -y
首先安装babel组件
代码语言:javascript复制npm install @babel/cli @babel/core @babel/preset-env babel-plugin-transform-es2015-modules-umd -D
配置babel,.babelrc(babel配置文件)
代码语言:javascript复制{
"presets": [
"@babel/preset-env"
],
"plugins": ["transform-es2015-modules-umd"]
}
package.json中添加babel命令
代码语言:javascript复制"scripts": {
"test": "echo "Error: no test specified" && exit 1",
"babel": "babel es6.js -o es5.js"
},
配置完成之后,执行命令即可
代码语言:javascript复制npm run babel
效果
根目录下的es6.js
代码语言:javascript复制export class A {
a = 0
constructor(v = 1) {
this.a = v
}
getA() {
return this.a
}
setA(v) {
this.a = v
}
}
const a = new A()
a.setA(10)
console.log(a);
执行npm run babel后,根目录生成es5.js
代码语言:javascript复制"use strict";
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["exports"], factory);
} else if (typeof exports !== "undefined") {
factory(exports);
} else {
var mod = {
exports: {}
};
factory(mod.exports);
global.undefined = mod.exports;
}
})(void 0, function (exports) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _defineProperties(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);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
var A = exports.A = function () {
function A() {
var v = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
_classCallCheck(this, A);
_defineProperty(this, "a", 0);
this.a = v;
}
_createClass(A, [{
key: "getA",
value: function getA() {
return this.a;
}
}, {
key: "setA",
value: function setA(v) {
this.a = v;
}
}]);
return A;
}();
var a = new A();
a.setA(10);
console.log(a);
});
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。