Apply your JS skills to key Mozilla projects as an MDN Fellow! http://mzl.la/MDNFellowship

mozilla

Revision 448005 of String.prototype.repeat

  • Revision slug: JavaScript/Reference/Global_Objects/String/repeat
  • Revision title: String.prototype.repeat
  • Revision id: 448005
  • Created:
  • Creator: zhangyaochun1987
  • Is current revision? No
  • Comment 完善一下

Revision Content

概述

返回一个重复当前字符串若干次数的新字符串.

Method of String
Implemented in JavaScript 1.8
ECMAScript Edition ECMAScript 6st Edition

语法

var newString = string.repeat(count);

参数

count
介于0和正无穷大之间的整数 : [ 0, +∞).

示例

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

浏览器兼容性

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatUnknown}} 24 {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} {{CompatUnknown}} 24 {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

 

Revision Source

<h2 id="Summary" name="Summary">概述</h2>
<p>返回一个重复当前字符串若干次数的新字符串.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of <a href="/en-US/docs/JavaScript/Reference/Global_Objects/String" title="JavaScript/Reference/Global_Objects/String"><code>String</code></a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.8</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>ECMAScript 6st Edition</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre class="syntaxbox">
<code><var>var newString = string</var>.repeat(<var>count</var>);</code></pre>
<h3 id="Parameters" name="Parameters">参数</h3>
<dl>
  <dt>
    <code>count</code></dt>
  <dd>
    介于0和正无穷大之间的整数 : [ 0, +∞).</dd>
</dl>
<h2 id="Examples" name="Examples">示例</h2>
<pre class="brush:js">
<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="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2>
<div>
  {{CompatibilityTable}}</div>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatUnknown}}</td>
        <td>24</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Chrome for Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Mobile</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>24</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
      </tr>
    </tbody>
  </table>
</div>
<p>&nbsp;</p>
Revert to this revision