aboutsummaryrefslogtreecommitdiff
path: root/files/de/web/javascript/reference/global_objects/string/repeat/index.html
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:41:15 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:41:15 -0500
commit4b1a9203c547c019fc5398082ae19a3f3d4c3efe (patch)
treed4a40e13ceeb9f85479605110a76e7a4d5f3b56b /files/de/web/javascript/reference/global_objects/string/repeat/index.html
parent33058f2b292b3a581333bdfb21b8f671898c5060 (diff)
downloadtranslated-content-4b1a9203c547c019fc5398082ae19a3f3d4c3efe.tar.gz
translated-content-4b1a9203c547c019fc5398082ae19a3f3d4c3efe.tar.bz2
translated-content-4b1a9203c547c019fc5398082ae19a3f3d4c3efe.zip
initial commit
Diffstat (limited to 'files/de/web/javascript/reference/global_objects/string/repeat/index.html')
-rw-r--r--files/de/web/javascript/reference/global_objects/string/repeat/index.html286
1 files changed, 286 insertions, 0 deletions
diff --git a/files/de/web/javascript/reference/global_objects/string/repeat/index.html b/files/de/web/javascript/reference/global_objects/string/repeat/index.html
new file mode 100644
index 0000000000..6008654521
--- /dev/null
+++ b/files/de/web/javascript/reference/global_objects/string/repeat/index.html
@@ -0,0 +1,286 @@
+---
+title: String.prototype.repeat()
+slug: Web/JavaScript/Reference/Global_Objects/String/repeat
+tags:
+ - ECMAScript 2015
+ - JavaScript
+ - Méthode
+ - Prototype
+ - Referenz
+ - String
+translation_of: Web/JavaScript/Reference/Global_Objects/String/repeat
+---
+<div>{{JSRef}}</div>
+
+<p>Die <strong><code>repeat()</code></strong>-Funktion erzeugt und gibt eine Zeichenkette zurück, die die spezifizierte Anzahl von Kopien der angegebenen Zeichenkette aneinandergereiht enthält.</p>
+
+<h2 id="Syntax">Syntax</h2>
+
+<pre class="syntaxbox"><code><var>str</var>.repeat(<var>Anzahl</var>);</code>
+</pre>
+
+<h3 id="Parameter">Parameter</h3>
+
+<dl>
+ <dt><code>Anzahl</code></dt>
+ <dd>Eine Zahl zwischen 0 und +∞: [0, +∞), die die Anzahl Kopien der Ursprungszeichenkette in der neu erzeugten Zeichenkette definiert.</dd>
+</dl>
+
+<h3 id="Rückgabeergebnis">Rückgabeergebnis</h3>
+
+<p>Eine neue Zeichenkette mit der angegebenen Anzahl an Kopien der vorgegebenen Zeichenkette.</p>
+
+<h3 id="Ausnahmen">Ausnahmen</h3>
+
+<ul>
+ <li>{{jsxref("Errors/Negative_repetition_count", "RangeError")}}: die Wiederholungsanzahl darf nicht negativ sein.</li>
+ <li>{{jsxref("Errors/Resulting_string_too_large", "RangeError")}}: die Anzahl an Wiederholungen darf nicht unendlich sein und die maximale Zeichenkettenlänge nicht überschreiten.</li>
+</ul>
+
+<h2 id="Beispiele">Beispiele</h2>
+
+<pre class="brush: js">'abc'.repeat(-1); // Parameterfehler
+'abc'.repeat(0); // ''
+'abc'.repeat(1); // 'abc'
+'abc'.repeat(2); // 'abcabc'
+'abc'.repeat(3.5); // 'abcabcabc' (Anzahl wird in einen ganzzahligen Integer umgewandelt)
+'abc'.repeat(1/0); // Parameterfehler
+
+({ toString: () =&gt; 'abc', repeat: String.prototype.repeat }).repeat(2);
+// 'abcabc' (repeat() ist eine generische Methode)
+</pre>
+
+<h2 id="Polyfill">Polyfill</h2>
+
+<p>Diese Funktion wurde zur ECMAScript 2015 Spezifikation hinzugefügt und steht unter Umständen noch nicht in allen JavaScript-Implementierungen zur Verfügung. Bedarfsweise können Sie die Methode <code>String.prototype.repeat()</code> durch folgenden Code zur Verfügung stellen:</p>
+
+<pre class="brush: 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 '';
+ }
+ // Ensuring count is a 31-bit integer allows us to heavily optimize the
+ // main part. But anyway, most current (August 2014) browsers can't handle
+ // strings 1 &lt;&lt; 28 chars or longer, so:
+ if (str.length * count &gt;= 1 &lt;&lt; 28) {
+ throw new RangeError('repeat count must not overflow maximum string size');
+ }
+ var rpt = '';
+ for (var i = 0; i &lt; count; i++) {
+ rpt += str;
+ }
+ return rpt;
+ }
+}
+</pre>
+
+<h4 id="Polyfill_ES5">Polyfill ES5</h4>
+
+<pre class="syntaxbox">//#es5
+'use strict';
+(function(win){
+ var typeOf=(function(w){var f=function f(x){return typeof(x)},o=w.Symbol,p;if(o &amp;&amp; typeof(o)==='function' &amp;&amp; typeof(o.iterator)==='symbol'){p=o.prototype;f=function(x){return x &amp;&amp; x.constructor===o &amp;&amp; x!==p?'symbol':typeof x}};return f})(win),
+ exist=function(o,p,t){return p in o &amp;&amp; typeOf(o[p])===t};
+ (function(w){
+ var o=w.String.prototype;
+ if(!exist(o,'repeat','function')){o.repeat=(function(A,E){return function(n){var i=n&gt;&gt;0,s=this,l=s.length,j;if(i===0||l&lt;1){s=''}else{j=268435456;if(i&lt;0||i&gt;=j||i*l&gt;j){throw new RE('Invalidcountvalue')}else if(i&gt;0){s=A(++i).join(s)}};return s}})(w.Array,w.RangeError)};
+ })(win);
+})(window);
+
+//test:
+console.clear();
+console.log(
+'abc'.repeat(false),//''
+'abc'.repeat({}),//''
+'abc'.repeat([]),//''
+'abc'.repeat(['']),//''
+'abc'.repeat([0]),//''
+'abc'.repeat([0,1]),//''
+'abc'.repeat([1,1]),//''
+'abc'.repeat(0),//''
+'abc'.repeat(.6),//''
+'abc'.repeat(true),//'abc'
+'abc'.repeat(1),//'abc'
+'abc'.repeat(2),//'abcabc'
+'abc'.repeat([2]),//'abcabc'
+'abc'.repeat(3.5),//'abcabcabc'
+''.repeat(2)//''
+);
+console.log(
+'abc'.repeat(-Infinity),//RangeError: Invalid count value
+'abc'.repeat(Infinity),//RangeError: Invalid count value
+'abc'.repeat(1/0),//RangeError: Invalid count value
+'abc'.repeat(-1)//RangeError: Invalid count value
+);
+
+/*
+es5 src:
+'use strict';
+(function(win){
+
+ var typeOf=(function(w){var f=function f(x){return typeof(x)},o=w.Symbol,p;if(o &amp;&amp; typeof(o)==='function' &amp;&amp; typeof(o.iterator)==='symbol'){p=o.prototype;f=function(x){return x &amp;&amp; x.constructor===o &amp;&amp; x!==p?'symbol':typeof x}};return f})(win),
+ exist=function(o,p,t){return p in o &amp;&amp; typeOf(o[p])===t};
+
+ (function(w){
+ var o=w.String.prototype;
+ if(!exist(o,'repeat','function')){
+ o.repeat=(function(A,E){
+ return function(n){
+ var i=n&gt;&gt;0,s=this,l=s.length,j;
+ if(i===0||l&lt;1){s=''}else{
+ j=268435456;
+ if(i&lt;0||i&gt;=j||i*l&gt;j){throw new RE('Invalidcountvalue')}else if(i&gt;0){s=A(++i).join(s)}
+ };
+ return s
+ };
+ })(w.Array,w.RangeError);
+ };
+ //..
+ })(win);
+
+})(window);
+*/
+</pre>
+
+<h4 id="Polyfill_ES6">Polyfill ES6</h4>
+
+<pre class="syntaxbox">//#es6
+
+(w=&gt;{
+
+ const typeOf=(o=&gt;{let f=x=&gt;typeof x;if(o &amp;&amp; 'function'===typeof o){const s='symbol';if(s===typeof o.iterator){const p=o.prototype;f=x=&gt;x &amp;&amp; x.constructor===o &amp;&amp; x!==p?s:typeof x}};return f})(w.Symbol),
+
+ exist=(o,p,t)=&gt;p in o &amp;&amp; typeOf(o[p])===t;
+
+ (o=&gt;{
+
+ if(!exist(o,'repeat','function')){const A=w.Array,E=w.RangeError;o.repeat=function(n){var i=n&gt;&gt;0,s='';if(i!==0){let t=this;const l=t.length;if(l!==0){if(i&lt;0||i&gt;=(t=268435456)||i*l&gt;t){throw new E('Invalid count value')}else if(i&gt;0){s=A(++i).join(t)}}};return s}};
+
+ })(w.String.prototype);
+
+})(window);
+
+/*
+
+es6 src:
+
+(w=&gt;{
+
+ const typeOf=(o=&gt;{let f=x=&gt;typeof x;if(o &amp;&amp; 'function'===typeof o){const s='symbol';if(s===typeof o.iterator){const p=o.prototype;f=x=&gt;x &amp;&amp; x.constructor===o &amp;&amp; x!==p?s:typeof x}};return f})(w.Symbol),
+
+ exist=(o,p,t)=&gt;p in o &amp;&amp; typeOf(o[p])===t;
+
+
+ (o=&gt;{
+
+ if(!exist(o,'repeat','function')){
+
+ const A=w.Array;
+
+ o.repeat=function(n){var i=n&gt;&gt;0,s='';if(i!==0){let t=this;const l=t.length;if(l!==0){if(i&lt;0||i&gt;=(t=268435456)||i*l&gt;t){throw new RangeError('Invalid count value')}else if(i&gt;0){s=A(++i).join(t)}}};return s};
+
+ };
+
+ //..
+
+ })(w.String.prototype);
+
+
+})(window);
+
+*/
+
+
+//test:
+
+console.clear();
+
+console.log(
+
+'abc'.repeat(false),//''
+
+'abc'.repeat({}),//''
+
+'abc'.repeat([]),//''
+
+'abc'.repeat(['']),//''
+
+'abc'.repeat([0]),//''
+
+'abc'.repeat([0,1]),//''
+
+'abc'.repeat([1,1]),//''
+
+'abc'.repeat(0),//''
+
+'abc'.repeat(.6),//''
+
+'abc'.repeat(true),//'abc'
+
+'abc'.repeat(1),//'abc'
+
+'abc'.repeat(2),//'abcabc'
+
+'abc'.repeat([2]),//'abcabc'
+
+'abc'.repeat(3.5),//'abcabcabc'
+
+''.repeat(2)//''
+
+);
+
+console.log(
+
+'abc'.repeat(-Infinity),//RangeError: Invalid count value
+
+'abc'.repeat(Infinity),//RangeError: Invalid count value
+
+'abc'.repeat(1/0),//RangeError: Invalid count value
+
+'abc'.repeat(-1)//RangeError: Invalid count value
+
+);</pre>
+
+<h2 id="Spezifikationen">Spezifikationen</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Spezifikation</th>
+ <th scope="col">Status</th>
+ <th scope="col">Kommentar</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES2015', '#sec-string.prototype.repeat', 'String.prototype.repeat')}}</td>
+ <td>{{Spec2('ES2015')}}</td>
+ <td>Erste Definition.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-string.prototype.repeat', 'String.prototype.repeat')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browserkompatibilität">Browserkompatibilität</h2>
+
+<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>
+
+<p>{{Compat("javascript.builtins.String.repeat")}}</p>