---
title: "Javascript string repeat 重复拼写字符串"
author: "Nonkr"
date: "2013-07-17T17:31:45Z"
tags: []
categories: ["Web前端"]
slug: "javascript-string-repeat"
draft: false
lastmod: "2013-07-17T17:31:45Z"
id: 1908544
url: "https://nonkr.iteye.com/blog/1908544"
---

```
function repeat(s, n){
    var a = [];
    while(a.length < n){
        a.push(s);
    }
    return a.join('');
}

//或者

String.prototype.repeat = function( num )
{
    return new Array( num + 1 ).join( this );
}
```