aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/string/repeat
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/repeat
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/repeat')
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/string/repeat/index.html124
1 files changed, 124 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/repeat/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/repeat/index.html
new file mode 100644
index 0000000000..ce35065f1a
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/global_objects/string/repeat/index.html
@@ -0,0 +1,124 @@
+---
+title: String.prototype.repeat()
+slug: Web/JavaScript/Reference/Global_Objects/String/repeat
+tags:
+ - ECMAScript 2015
+ - ES 6
+ - JavaScript
+ - Method
+ - Prototype
+ - Reference
+ - String
+ - polyfill
+ - repeat()
+ - 填充
+translation_of: Web/JavaScript/Reference/Global_Objects/String/repeat
+---
+<p>{{JSRef}}</p>
+
+<p><strong><code>repeat()</code></strong> 构造并返回一个新字符串,该字符串包含被连接在一起的指定数量的字符串的副本。</p>
+
+<h2 id="Syntax" name="Syntax">语法</h2>
+
+<pre class="syntaxbox notranslate"><code><var>str</var>.repeat(<var>count</var>)</code></pre>
+
+<h3 id="Parameters" name="Parameters">参数</h3>
+
+<dl>
+ <dt><code>count</code></dt>
+ <dd>介于 <code>0</code> 和 {{jsxref("Global_Objects/Number/POSITIVE_INFINITY", "+Infinity")}} 之间的整数。表示在新构造的字符串中重复了多少遍原字符串。</dd>
+</dl>
+
+<h3 id="返回值">返回值</h3>
+
+<p> 包含指定字符串的指定数量副本的新字符串。</p>
+
+<h3 id="Exceptions">Exceptions</h3>
+
+<ul>
+ <li>{{jsxref("Errors/Negative_repetition_count", "RangeError")}}: 重复次数不能为负数。</li>
+ <li>{{jsxref("Errors/Resulting_string_too_large", "RangeError")}}: 重复次数必须小于 infinity,且长度不会大于最长的字符串。</li>
+</ul>
+
+<h2 id="兼容补丁Polyfill">兼容补丁(Polyfill)</h2>
+
+<p>此方法已添加到 ECMAScript 2015 规范中,并且可能尚未在所有 JavaScript 实现中可用。然而,你可以使用以下代码段对 String.prototype.repeat() 进行填充:</p>
+
+<pre class="brush: js language-js notranslate"><code class="language-js"><span class="keyword token">if</span> <span class="punctuation token">(</span><span class="operator token">!</span>String<span class="punctuation token">.</span>prototype<span class="punctuation token">.</span>repeat<span class="punctuation token">)</span> <span class="punctuation token">{</span>
+ String<span class="punctuation token">.</span>prototype<span class="punctuation token">.</span>repeat <span class="operator token">=</span> <span class="keyword token">function</span><span class="punctuation token">(</span>count<span class="punctuation token">)</span> <span class="punctuation token">{</span>
+ <span class="string token">'use strict'</span><span class="punctuation token">;</span>
+ <span class="keyword token">if</span> <span class="punctuation token">(</span><span class="keyword token">this</span> <span class="operator token">==</span> <span class="keyword token">null</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
+ <span class="keyword token">throw</span> <span class="keyword token">new</span> <span class="class-name token">TypeError</span><span class="punctuation token">(</span><span class="string token">'can\'t convert '</span> <span class="operator token">+</span> <span class="keyword token">this</span> <span class="operator token">+</span> <span class="string token">' to object'</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
+ <span class="punctuation token">}</span>
+ <span class="keyword token">var</span> str <span class="operator token">=</span> <span class="string token">''</span> <span class="operator token">+</span> <span class="keyword token">this</span><span class="punctuation token">;</span>
+ count <span class="operator token">=</span> <span class="operator token">+</span>count<span class="punctuation token">;</span>
+ <span class="keyword token">if</span> <span class="punctuation token">(</span>count <span class="operator token">!</span><span class="operator token">=</span> count<span class="punctuation token">)</span> <span class="punctuation token">{</span>
+ count <span class="operator token">=</span> <span class="number token">0</span><span class="punctuation token">;</span>
+ <span class="punctuation token">}</span>
+ <span class="keyword token">if</span> <span class="punctuation token">(</span>count <span class="operator token">&lt;</span> <span class="number token">0</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
+ <span class="keyword token">throw</span> <span class="keyword token">new</span> <span class="class-name token">RangeError</span><span class="punctuation token">(</span><span class="string token">'repeat count must be non-negative'</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
+ <span class="punctuation token">}</span>
+ <span class="keyword token">if</span> <span class="punctuation token">(</span>count <span class="operator token">==</span> <span class="number token">Infinity</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
+ <span class="keyword token">throw</span> <span class="keyword token">new</span> <span class="class-name token">RangeError</span><span class="punctuation token">(</span><span class="string token">'repeat count must be less than infinity'</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
+ <span class="punctuation token">}</span>
+ count <span class="operator token">=</span> Math<span class="punctuation token">.</span><span class="function token">floor<span class="punctuation token">(</span></span>count<span class="punctuation token">)</span><span class="punctuation token">;</span>
+ <span class="keyword token">if</span> <span class="punctuation token">(</span>str<span class="punctuation token">.</span>length <span class="operator token">==</span> <span class="number token">0</span> <span class="operator token">||</span> count <span class="operator token">==</span> <span class="number token">0</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
+ <span class="keyword token">return</span> <span class="string token">''</span><span class="punctuation token">;</span>
+ <span class="punctuation token">}</span>
+ <span class="comment token"> // 确保 count 是一个 31 位的整数。这样我们就可以使用如下优化的算法。
+ // 当前(2014年8月),绝大多数浏览器都不能支持 1 &lt;&lt; 28 长的字符串,所以:</span><span class="comment token">
+</span> <span class="keyword token">if</span> <span class="punctuation token">(</span>str<span class="punctuation token">.</span>length <span class="operator token">*</span> count <span class="operator token">&gt;=</span> <span class="number token">1</span> <span class="operator token">&lt;</span><span class="operator token">&lt;</span> <span class="number token">28</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
+ <span class="keyword token">throw</span> <span class="keyword token">new</span> <span class="class-name token">RangeError</span><span class="punctuation token">(</span><span class="string token">'repeat count must not overflow maximum string size'</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
+ <span class="punctuation token">}</span>
+ <span class="keyword token">var</span> rpt <span class="operator token">=</span> <span class="string token">''</span><span class="punctuation token">;</span>
+ <span class="keyword token">for</span> <span class="punctuation token">(</span><span class="punctuation token">;</span><span class="punctuation token">;</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
+ <span class="keyword token">if</span> <span class="punctuation token">(</span><span class="punctuation token">(</span>count <span class="operator token">&amp;</span> <span class="number token">1</span><span class="punctuation token">)</span> <span class="operator token">==</span> <span class="number token">1</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
+ rpt <span class="operator token">+</span><span class="operator token">=</span> str<span class="punctuation token">;</span>
+ <span class="punctuation token">}</span>
+ count <span class="operator token">&gt;</span><span class="operator token">&gt;</span><span class="operator token">&gt;=</span> <span class="number token">1</span><span class="punctuation token">;</span>
+ <span class="keyword token">if</span> <span class="punctuation token">(</span>count <span class="operator token">==</span> <span class="number token">0</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
+ <span class="keyword token">break</span><span class="punctuation token">;</span>
+ <span class="punctuation token">}</span>
+ str <span class="operator token">+</span><span class="operator token">=</span> str<span class="punctuation token">;</span>
+ <span class="punctuation token">}</span>
+ <span class="keyword token">return</span> rpt<span class="punctuation token">;</span>
+ <span class="punctuation token">}</span>
+<span class="punctuation token">}</span></code></pre>
+
+
+<h2 id="Examples" name="Examples">示例</h2>
+
+<pre class="brush:js notranslate"><span class="difflineplus">"abc".repeat(-1) // </span>RangeError: repeat count must be positive and less than inifinity
+<span class="difflineplus">"abc".repeat(0) // ""</span>
+<span class="difflineplus">"abc".repeat(1) // "abc"</span>
+<span class="difflineplus">"abc".repeat(2) // "</span>abcabc<span class="difflineplus">"
+</span>"abc".repeat(3.5)<span class="difflineplus"> // "abcabcabc" 参数count将会被自动转换成整数.
+</span><span class="difflineplus">"abc".repeat(1/0) //</span> RangeError: repeat count must be positive and less than inifinity
+
+<span class="difflineplus">({toString : () =&gt; "abc", repeat : String.prototype.repeat}</span><span class="difflineplus">).repeat(2)
+</span><span class="difflineplus">//"</span>abcabc<span class="difflineplus">",repeat是一个通用方法</span>,也就是它的调用者可以不是一个字符串对象.</pre>
+
+<h2 id="规范">规范</h2>
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">规范</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-string.prototype.repeat', 'String.prototype.repeat')}}</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2>
+
+<p>{{Compat("javascript.builtins.String.repeat")}}</p>
+
+<h2 id="相关链接">相关链接</h2>
+
+<ul>
+ <li>{{jsxref("String.prototype.concat()")}}</li>
+</ul>