BabelJS教程

ES8 功能转换ES5

字符串填充是添加到 javascript 的新 ES8 特性。我们将研究一个简单的例子,它将使用 babel 将字符串填充转换为 ES5、

字符串填充

字符串填充根据指定的长度从左侧添加另一个字符串。字符串填充的语法如下所示-

语法

str.padStart(length, string);
str.padEnd(length, string);

示例

const str = 'abc';
console.log(str.padStart(8, '_'));
console.log(str.padEnd(8, '_'));

输出

_____abc
abc_____

ES8-字符串填充

const str = 'abc';
console.log(str.padStart(8, '_'));
console.log(str.padEnd(8, '_'));

命令

npx babel strpad.js--out-file strpad_es5.js

巴别塔-ES5

'use strict';
var str = 'abc';
console.log(str.padStart(8, '_'));
console.log(str.padEnd(8, '_'));
js 必须与 babel-polyfill 一起使用,如下所示-

test.html

<!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>
字符串填充
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4