Babeljs-transpile-es8-features-to-es5

提供:Dev Guides
移動先:案内検索

BabelJS-ES5へのES8のトランスパイル機能

文字列パディングは、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とともに使用する必要があります-

testl

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

文字列パディング