BabelJS - 将 ES7 功能转换为 ES5

BabelJS - 将 ES7 功能转换为 ES5

在本文中,我们将学习如何将 ES7 功能转换为 ES5。

ECMA Script 7 添加了以下新功能

  • Async-Await
  • Exponentiation Operator
  • Array.prototype.includes()

我们将使用 babeljs 将它们编译为 ES5。根据您的项目要求,也可以在任何 ecma 版本中编译代码,即 ES7 到 ES6 或 ES7 到 ES5。由于 ES5 版本是最稳定的,并且在所有现代和旧浏览器上都可以正常工作,因此我们将代码编译为 ES5。

 

Async-Await

Async 是一个异步函数,它返回一个隐式的承诺。承诺要么被解决,要么被拒绝。异步函数与普通的标准函数相同。该函数可以有 await 表达式,它会暂停执行,直到它返回一个 Promise,一旦它得到它,执行就会继续。只有当函数是异步的时,Await 才会起作用。

这是一个关于 async 和 await 的工作示例。

let timer = () => {
   return new Promise(resolve => {
      setTimeout(() => {
         resolve("Promise resolved after 5 seconds");
      }, 5000);
   });
};
let out = async () => {
   let msg = await timer();
   console.log(msg);
   console.log("hello after await");
};
out();

输出:

Promise resolved after 5 seconds
hello after await

await 表达式是在调用计时器函数之前添加的。计时器函数将在 5 秒后返回承诺。因此 await 将暂停执行,直到计时器功能上的承诺被解决或拒绝,然后再继续。

现在让我们使用 babel 将上述代码转换为 ES5。

npx babel asyncawait.js --out-file asyncawait_es5.js
"use strict";

var timer = function timer() {
   return new Promise(function (resolve) {
      setTimeout(function () {
         resolve("Promise resolved after 5 seconds");
      }, 5000);
   });
};
var out = async function out() {
   var msg = await timer();
   console.log(msg);
   console.log("hello after await");
};

out();

Babeljs 不编译对象或方法;所以这里使用的 Promise 不会被转译,而是按原样显示。为了在旧浏览器上支持 Promise,我们需要添加支持 Promise 的代码。现在,让我们按如下方式安装 babel-polyfill 

npm install --save babel-polyfill

它应该保存为依赖而不是开发依赖。

要在浏览器中运行代码,我们将使用 node_modules\babel-polyfill\dist\polyfill.min.js 中的 polyfill 文件,并使用 script 标签调用它,如下所示

<!DOCTYPE html>
<html>
   <head>
      <title>BabelJs Testing</title>
   </head>
   <body>
      <script src="node_modules\babel-polyfill\dist\polyfill.min.js" type="text/javascript"></script>
      <script type="text/javascript" src="aynscawait_es5.js"></script>
   </body>
</html>

 

Exponentiation Operator

** 是 ES7 中用于求幂的运算符。以下示例显示了 ES7 中相同的工作,代码使用 babeljs 进行了转译。

let sqr = 9 ** 2;
console.log(sqr);

输出:

81

要转换幂运算符,我们需要安装一个插件,如下所示

npm install --save-dev babel-plugin-transform-exponentiation-operator

将插件详细信息添加到 .babelrc 文件中,如下所示

{
   "presets":[
      "es2015"
   ],
   "plugins": ["transform-exponentiation-operator"]
}
npx babel exponeniation.js --out-file exponeniation_es5.js
"use strict";

var sqr = Math.pow(9, 2);
console.log(sqr);

Array.prototype.includes()

如果传递给它的元素存在于数组中,则此功能为 true,否则为 false。

let arr1 = [10, 6, 3, 9, 17];
console.log(arr1.includes(9));
let names = ['Siya', 'Tom', 'Jerry', 'Bean', 'Ben'];
console.log(names.includes('Tom'));
console.log(names.includes('Be'));
true
true
false

我们必须在这里再次使用 babel-polyfill,因为 include 是数组上的一个方法,它不会被转译。我们需要额外的步骤来包含 polyfill 以使其在旧浏览器中工作。

npx babel array_include.js --out-file array_include_es5.js
'use strict';

var arr1 = [10, 6, 3, 9, 17];
console.log(arr1.includes(9));
var names = ['Siya', 'Tom', 'Jerry', 'Bean', 'Ben'];
console.log(names.includes('Tom'));
console.log(names.includes('Be'));

要在旧版浏览器中测试它,我们需要使用 polyfill,如下所示

<!DOCTYPE html>
<html>
   <head>
      <title>BabelJs Testing</title>
   </head>
   <body>
      <script src="node_modules\babel-polyfill\dist\polyfill.min.js" type="text/javascript"></script>
      <script type="text/javascript" src="array_include_es5.js"></script>
   </body>
</html>

 

版权声明:著作权归作者所有。

thumb_up 0 | star_outline 0 | textsms 0