uTools-Manuals/docs/javascript/Reference/Errors/Negative_repetition_count.html
2019-04-21 11:50:48 +08:00

24 lines
1.8 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<article id="wikiArticle">
<div></div>
<h2 id="信息">信息</h2>
<pre><code class="language-javascript">RangeError: repeat count must be non-negative (Firefox)
RangeError: Invalid count value (Chrome)
</code></pre>
<h2 id="错误类型">错误类型</h2>
<p><a href="Reference/Global_Objects/RangeError" title="RangeError对象标明一个错误当一个值不在其所允许的范围或者集合中。"><code>RangeError</code></a></p>
<h2 id="发生了什么">发生了什么?</h2>
<p>代码中使用了 <a href="Reference/Global_Objects/String/repeat" title="repeat() 构造并返回一个新字符串,该字符串包含被连接在一起的指定数量的字符串的副本。"><code>String.prototype.repeat()</code></a>方法。<span class="short_text" id="result_box" lang="zh-CN"><span>它有一个计数参数,表示重复该字符串的次数</span></span>。该参数必须在 0 及正 <a href="Reference/Global_Objects/Infinity" title="全局属性 Infinity 是一个数值,表示无穷大。"><code>Infinity</code></a> 之间,且不能为负数。该值的合法范围可以这样表示: [0, +∞)。</p>
<h2 id="示例">示例</h2>
<h3 id="无效的">无效的</h3>
<pre><code class="language-js example-bad">'abc'.repeat(-1); // RangeError </code></pre>
<h3 id="有效的">有效的</h3>
<pre><code class="language-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)
</code></pre>
<h2 id="See_also">See also</h2>
<ul>
<li><a href="Reference/Global_Objects/String/repeat" title="repeat() 构造并返回一个新字符串,该字符串包含被连接在一起的指定数量的字符串的副本。"><code>String.prototype.repeat()</code></a></li>
</ul>
</article>