美文网首页web前端开发
关于Babel 6的 loose mode

关于Babel 6的 loose mode

作者: 忽如寄 | 来源:发表于2018-05-24 18:17 被阅读100次

1.Overview

loose mode 我翻译为松散模式,loose mode在babel中通常是不推荐使用的,但是我们需要知道的是使用 loose mode 转换而来的代码更加像ES5的代码(更像是人手写的)

大多数Babel插件都有两种模式 normal modeloose modenormal mode 转换而来的ES5代码更加符合ECMAScript 6 的语义,而 loose mode 转换而来的代码更加简单,更像是人写的。

loose mode 的优点在于兼容旧引擎,可能会更加快,缺点在于如果我们需要将转换之后的代码重新转换为 native ES6 代码,可能会遇到问题,而这个冒险在大多数时候是不值得的。

2.Examples

以ES6的class为例,我们编写以下代码:

class person {
  constractor(name, age) {
    this.name = name;
    this.age = age;
  }
  getName(){
    return this.name;
  }
  getAge(){
      return this.age;
  }
}

normal mode 下转换为:

"use strict";

var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var person = function () {
  function person() {
    _classCallCheck(this, person);
  }

  _createClass(person, [{
    key: "constractor",
    value: function constractor(name, age) {
      this.name = name;
      this.age = age;
    }
  }, {
    key: "getName",
    value: function getName() {
      return this.name;
    }
  }, {
    key: "getAge",
    value: function getAge() {
      return this.age;
    }
  }]);

  return person;
}();

loose mode 下转换为:

"use strict";

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var person = function () {
  function person() {
    _classCallCheck(this, person);
  }

  person.prototype.constractor = function constractor(name, age) {
    this.name = name;
    this.age = age;
  };

  person.prototype.getName = function getName() {
    return this.name;
  };

  person.prototype.getAge = function getAge() {
    return this.age;
  };

  return person;
}();

可以看到,非常直观的是 loose mode 下的代码更加像是人手写的。尽管如此,但是仍然不推荐使用 loose mode

相关文章

  • 关于Babel 6的 loose mode

    1.Overview loose mode 我翻译为松散模式,loose mode在babel中通常是不推荐使用的...

  • 5.调试react-native项目

    安装babel: 一个能把ES6语法转成ES5的工具。 1.报错Strict mode does not allo...

  • ES6 转码为 ES5 (Babel)

    Babel 6 与之前版本的区别 之前版本只需安装一个 babel,但在 babel 6 中,将 babel 拆分...

  • nas 双网卡配置文档

    双网卡绑定的模式 关于mode共有0-6等7种模式,mode的值表示工作模式, 他共有0,1,2,3,4,5,6这...

  • 了解 Babel 各个模块

    了解 Babel 各个模块 本文所研究的是 babel 6 版本。babel 6 是 2015年10月30号 发布...

  • babel 常见用法

    1. babel 5.x 和babel 6.x babel 5.x -> 6.x 的变化非常大,15 年 11 月...

  • babel6 升级总结

    随着越来越多的库升级至 babel6,将项目升级至 babel6 迫在眉睫。在这里总结将 项目升级至 babel6...

  • 安装Babel,es6转es5

    安装Babel(命令行环境,针对Babel6.x版本)1、首先安装babel-cli(用于在终端使用babel) ...

  • [Mark]使用 babel-6 进行开发

    标签: es6 babel-6 (https://babeljs.io)发布了。babel-6 可以直接使用 ba...

  • 5.es6转es5

    1.安装依赖 cnpm i -D babel-core@6 babel-loader@7 babel-plugin...

网友评论

    本文标题:关于Babel 6的 loose mode

    本文链接:https://www.haomeiwen.com/subject/vhasjftx.html