BabelJS - 将 ES8 功能转换为 ES5

字符串填充是添加到 javascript 中的新 ES8 功能。我们将研究一个简单的示例,它将使用 babel 将字符串填充转换为 ES5。
String Padding
字符串填充根据指定的长度从左侧添加另一个字符串。字符串填充的语法如下所示:
str.padStart(length, string);
str.padEnd(length, string);
const str = 'abc';
console.log(str.padStart(8, '_'));
console.log(str.padEnd(8, '_'));
输出:
_____abc
abc_____
npx babel strpad.js --out-file strpad_es5.js
编译后:
'use strict';
var str = 'abc';
console.log(str.padStart(8, '_'));
console.log(str.padEnd(8, '_'));
js 必须与 babel-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="strpad_es5.js"></script>
</body>
</html>
输出如下:
