aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/string/padstart
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/string/padstart
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/string/padstart')
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/string/padstart/index.html102
1 files changed, 102 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/padstart/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/padstart/index.html
new file mode 100644
index 0000000000..24431715d0
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/global_objects/string/padstart/index.html
@@ -0,0 +1,102 @@
+---
+title: String.prototype.padStart()
+slug: Web/JavaScript/Reference/Global_Objects/String/padStart
+tags:
+ - ECMAScript 2017
+ - JavaScript
+ - Method
+ - Reference
+ - String
+ - String.padStart()
+translation_of: Web/JavaScript/Reference/Global_Objects/String/padStart
+---
+<div>{{JSRef}}</div>
+
+<p><strong><code>padStart()</code></strong> 方法用另一个字符串填充当前字符串(如果需要的话,会重复多次),以便产生的字符串达到给定的长度。从当前字符串的左侧开始填充。</p>
+
+<div>{{EmbedInteractiveExample("pages/js/string-padstart.html")}}</div>
+
+<div class="hidden">The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> and send us a pull request.</div>
+
+<h2 id="语法">语法</h2>
+
+<pre class="syntaxbox notranslate"><var>str</var>.padStart(<var>targetLength</var> [, <var>padString</var>])</pre>
+
+<h3 id="参数">参数</h3>
+
+<dl>
+ <dt><code>targetLength</code></dt>
+ <dd>当前字符串需要填充到的目标长度。如果这个数值小于当前字符串的长度,则返回当前字符串本身。</dd>
+ <dt><code>padString</code> {{optional_inline}}</dt>
+ <dd>填充字符串。如果字符串太长,使填充后的字符串长度超过了目标长度,则只保留最左侧的部分,其他部分会被截断。此参数的默认值为 " "(U+0020)。</dd>
+</dl>
+
+<h3 id="返回值">返回值</h3>
+
+<p>在原字符串开头填充指定的填充字符串直到目标长度所形成的新字符串。</p>
+
+<h2 id="示例">示例</h2>
+
+<pre class="brush: js line-numbers language-js notranslate"><code class="language-js">'abc'.padStart(10); // " abc"
+'abc'.padStart(10, "foo"); // "foofoofabc"
+'abc'.padStart(6,"123465"); // "123abc"
+'abc'.padStart(8, "0"); // "00000abc"
+'abc'.padStart(1); // "abc"</code></pre>
+
+<h2 id="Polyfill">Polyfill</h2>
+
+<p>如果原生环境不支持该方法,在其他代码之前先运行下面的代码,将创建 <code>String.prototype.padStart()</code> 方法。</p>
+
+<pre class="brush: js line-numbers language-js notranslate"><code class="language-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&gt;&gt;0; //floor if number or convert non-number to 0;
+ padString = String((typeof padString !== 'undefined' ? padString : ' '));
+ if (this.length &gt; targetLength) {
+ return String(this);
+ }
+ else {
+ targetLength = targetLength-this.length;
+ if (targetLength &gt; 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);
+ }
+ };
+}</code>
+</pre>
+
+<h2 id="规范">规范</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">规范</th>
+ <th scope="col">状态</th>
+ <th scope="col">备注</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-string.prototype.padstart', 'String.prototype.padStart')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td>在 ECMAScript 2017 中首次被定义。</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES8', '#sec-string.prototype.padstart', 'String.prototype.padStart')}}</td>
+ <td>{{Spec2('ES8')}}</td>
+ <td></td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>
+
+<p>{{Compat("javascript.builtins.String.padStart")}}</p>
+
+<h2 id="另请参阅">另请参阅</h2>
+
+<ul>
+ <li>{{jsxref("String.prototype.padEnd()")}}</li>
+</ul>