From a6e2b313417471e4a3f7cd19aca7d9224ead6359 Mon Sep 17 00:00:00 2001 From: YangQi Date: Wed, 7 Sep 2022 19:14:55 +0800 Subject: [PATCH] docs: update padstart() and padend() (#8353) * docs: update padstart() and padend() * Apply suggestions from code review Co-authored-by: A1lo --- .../global_objects/string/padend/index.md | 34 +++------------ .../global_objects/string/padstart/index.md | 43 ++++++++----------- 2 files changed, 25 insertions(+), 52 deletions(-) diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/padend/index.md b/files/zh-cn/web/javascript/reference/global_objects/string/padend/index.md index 9b19290e988702..2c735cbc20180b 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/string/padend/index.md +++ b/files/zh-cn/web/javascript/reference/global_objects/string/padend/index.md @@ -10,8 +10,9 @@ slug: Web/JavaScript/Reference/Global_Objects/String/padEnd ## 语法 -```plain -str.padEnd(targetLength [, padString]) +```js +padEnd(targetLength) +padEnd(targetLength, padString) ``` ### 参数 @@ -34,31 +35,6 @@ str.padEnd(targetLength [, padString]) 'abc'.padEnd(1); // "abc" ``` -## Polyfill - -如果原生环境不支持该方法,在其他代码之前先运行下面的代码,将创建 `String.prototype.padEnd()` 方法。 - -```js -// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js -// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd -if (!String.prototype.padEnd) { - String.prototype.padEnd = function padEnd(targetLength,padString) { - targetLength = targetLength>>0; //floor if number or convert non-number to 0; - padString = String((typeof padString !== 'undefined' ? padString: '')); - if (this.length > targetLength) { - return String(this); - } - else { - targetLength = targetLength-this.length; - if (targetLength > padString.length) { - padString += padString.repeat(targetLength/padString.length); //append to original to ensure we are longer than needed - } - return String(this) + padString.slice(0,targetLength); - } - }; -} -``` - ## 规范 {{Specifications}} @@ -67,6 +43,8 @@ if (!String.prototype.padEnd) { {{Compat}} -## 另请参阅 +## 参见 +- [Polyfill of `String.prototype.padEnd` in `core-js`](https://github.com/zloirock/core-js#ecmascript-string-and-regexp) - {{jsxref("String.prototype.padStart()")}} +- [A polyfill](https://github.com/behnammodi/polyfill/blob/master/string.polyfill.js) diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/padstart/index.md b/files/zh-cn/web/javascript/reference/global_objects/string/padstart/index.md index 3d934bfe8a0576..7addb7834df5e1 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/string/padstart/index.md +++ b/files/zh-cn/web/javascript/reference/global_objects/string/padstart/index.md @@ -4,14 +4,15 @@ slug: Web/JavaScript/Reference/Global_Objects/String/padStart --- {{JSRef}} -**`padStart()`** 方法用另一个字符串填充当前字符串 (如果需要的话,会重复多次),以便产生的字符串达到给定的长度。从当前字符串的左侧开始填充。 +**`padStart()`** 方法用另一个字符串填充当前字符串(如果需要的话,会重复多次),以便产生的字符串达到给定的长度。从当前字符串的左侧开始填充。 {{EmbedInteractiveExample("pages/js/string-padstart.html")}} ## 语法 -```plain -str.padStart(targetLength [, padString]) +```js +padStart(targetLength) +padStart(targetLength, padString) ``` ### 参数 @@ -27,6 +28,8 @@ str.padStart(targetLength [, padString]) ## 示例 +### 简单示例 + ```js 'abc'.padStart(10); // " abc" 'abc'.padStart(10, "foo"); // "foofoofabc" @@ -35,29 +38,18 @@ str.padStart(targetLength [, padString]) 'abc'.padStart(1); // "abc" ``` -## Polyfill - -如果原生环境不支持该方法,在其他代码之前先运行下面的代码,将创建 `String.prototype.padStart()` 方法。 +### 将数字转换为固定长度的字符串 ```js -// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js -// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart -if (!String.prototype.padStart) { - String.prototype.padStart = function padStart(targetLength,padString) { - targetLength = targetLength>>0; //floor if number or convert non-number to 0; - padString = String((typeof padString !== 'undefined' ? padString : ' ')); - if (this.length > targetLength) { - return String(this); - } - else { - targetLength = targetLength-this.length; - if (targetLength > padString.length) { - padString += padString.repeat(targetLength/padString.length); //append to original to ensure we are longer than needed - } - return padString.slice(0,targetLength) + String(this); - } - }; +// JavaScript version of: (unsigned) +// printf "%0*d" width num +function leftFillNum(num, targetLength) { + return num.toString().padStart(targetLength, 0); } + +const num = 123; +console.log(leftFillNum(num, 5)); +// 预期输出: "00123" ``` ## 规范 @@ -68,6 +60,9 @@ if (!String.prototype.padStart) { {{Compat}} -## 另请参阅 +## 参见 +- [Polyfill of `String.prototype.padStart` in `core-js`](https://github.com/zloirock/core-js#ecmascript-string-and-regexp) - {{jsxref("String.prototype.padEnd()")}} +- [A polyfill](https://github.com/behnammodi/polyfill/blob/master/string.polyfill.js) +