BabelJS - ES5的Transpile ES8功能

字符串填充是添加到javascript的新ES8功能。我们将使用简单的示例,它将使用babel将字符串填充转换为ES5。

字符串填充

字符串填充从左侧添加另一个字符串按照规定的长度。字符串填充的语法如下所示−

语法

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

示例

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

输出

 
_____ abc
abc_____

ES8 - String Padding

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

命令

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

Babel - 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>

String Padding