aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/errors/negative_repetition_count/index.html
blob: 94a79e94a8d461266b9f885b358ffdff4549fb41 (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
---
title: 'RangeError: repeat count must be non-negative'
slug: Web/JavaScript/Reference/Errors/Negative_repetition_count
tags:
  - Errors
  - JavaScript
  - RangeError
  - 错误
translation_of: Web/JavaScript/Reference/Errors/Negative_repetition_count
---
<div>{{jsSidebar("Errors")}}</div>

<h2 id="信息">信息</h2>

<pre class="syntaxbox">RangeError: repeat count must be non-negative (Firefox)
RangeError: Invalid count value (Chrome)
</pre>

<h2 id="错误类型">错误类型</h2>

<p>{{jsxref("RangeError")}}</p>

<h2 id="发生了什么">发生了什么?</h2>

<p>代码中使用了 {{jsxref("String.prototype.repeat()")}}方法。它有一个计数参数,表示重复该字符串的次数。该参数必须在 0 及正 {{jsxref("Infinity")}} 之间,且不能为负数。该值的合法范围可以这样表示: [0, +∞)。</p>

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

<h3 id="无效的">无效的</h3>

<pre class="brush: js example-bad">'abc'.repeat(-1); // RangeError </pre>

<h3 id="有效的">有效的</h3>

<pre class="brush: js example-good">'abc'.repeat(0);    // ''
'abc'.repeat(1);    // 'abc'
'abc'.repeat(2);    // 'abcabc'
'abc'.repeat(3.5);  // 'abcabcabc' (count will be converted to integer)
</pre>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{jsxref("String.prototype.repeat()")}}</li>
</ul>