aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/string/repeat/index.html
blob: b880c16b922fccc3e413cb055bdb786f81276b79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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">if (!String.prototype.repeat) {
  String.prototype.repeat = function(count) {
    'use strict';
    if (this == null) {
      throw new TypeError('can\'t convert ' + this + ' to object');
    }
    var str = '' + this;
    count = +count;
    if (count != count) {
      count = 0;
    }
    if (count &lt; 0) {
      throw new RangeError('repeat count must be non-negative');
    }
    if (count == Infinity) {
      throw new RangeError('repeat count must be less than infinity');
    }
    count = Math.floor(count);
    if (str.length == 0 || count == 0) {
      return '';
    }
    // 确保 count 是一个 31 位的整数。这样我们就可以使用如下优化的算法。
    // 当前(2014年8月),绝大多数浏览器都不能支持 1 &lt;&lt; 28 长的字符串,所以:
    if (str.length * count &gt;= 1 &lt;&lt; 28) {
      throw new RangeError('repeat count must not overflow maximum string size');
    }
    var rpt = '';
    for (;;) {
      if ((count &amp; 1) == 1) {
        rpt += str;
      }
      count &gt;&gt;&gt;= 1;
      if (count == 0) {
        break;
      }
      str += str;
    }
    return rpt;
  }
}</code></pre>


<h2 id="Examples" name="Examples">示例</h2>

<pre class="brush:js notranslate">"abc".repeat(-1)     // RangeError: repeat count must be positive and less than inifinity
"abc".repeat(0)      // ""
"abc".repeat(1)      // "abc"
"abc".repeat(2)      // "abcabc"
"abc".repeat(3.5)    // "abcabcabc" 参数count将会被自动转换成整数.
"abc".repeat(1/0)    // RangeError: repeat count must be positive and less than inifinity

({toString : () =&gt; "abc", repeat : String.prototype.repeat}).repeat(2)
//"abcabc",repeat是一个通用方法,也就是它的调用者可以不是一个字符串对象.</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>