es6转es5_es6转es5插件

es6转es5_es6转es5插件步骤初始化环境npminit-y首先安装babel组件npminstall@babel/cli@babel/core@babel/preset-envbabel-plugin-transform-es2015-modules-umd-D配置babel,.babelrc(babel配置文件){“presets”:[“@babel/preset-env”],”plugins”:[“transform-es2015-modules-umd”]}

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

步骤

初始化环境

npm init -y

首先安装babel组件

npm install @babel/cli @babel/core @babel/preset-env babel-plugin-transform-es2015-modules-umd -D

在这里插入图片描述
配置babel,.babelrc(babel配置文件)

{
  "presets": [
    "@babel/preset-env"
  ],
  "plugins": ["transform-es2015-modules-umd"]
}

package.json中添加babel命令

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "babel": "babel es6.js -o es5.js"
  },

配置完成之后,执行命令即可

npm run babel

效果

根目录下的es6.js

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

"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);
});

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/189647.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号