Skip to content

Commit

Permalink
docs: update padstart() and padend() (#8353)
Browse files Browse the repository at this point in the history
* docs: update padstart() and padend()

* Apply suggestions from code review

Co-authored-by: A1lo <yin199909@aliyun.com>
  • Loading branch information
YangFong and yin1999 authored Sep 7, 2022
1 parent e7fc8d2 commit a6e2b31
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ slug: Web/JavaScript/Reference/Global_Objects/String/padEnd

## 语法

```plain
str.padEnd(targetLength [, padString])
```js
padEnd(targetLength)
padEnd(targetLength, padString)
```

### 参数
Expand All @@ -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}}
Expand All @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```

### 参数
Expand All @@ -27,6 +28,8 @@ str.padStart(targetLength [, padString])

## 示例

### 简单示例

```js
'abc'.padStart(10); // " abc"
'abc'.padStart(10, "foo"); // "foofoofabc"
Expand All @@ -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"
```

## 规范
Expand All @@ -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)

0 comments on commit a6e2b31

Please sign in to comment.