String.prototype.repeat()
String
值的 repeat()
方法构造并返回一个新字符串,其中包含该字符串的指定数量的副本,并连接在一起。
¥The repeat()
method of String
values constructs and returns a new string
which contains the specified number of copies of this string, concatenated together.
Try it
语法
参数
返回值
例外情况
示例
使用重复()
¥Using repeat()
js
"abc".repeat(-1); // RangeError
"abc".repeat(0); // ''
"abc".repeat(1); // 'abc'
"abc".repeat(2); // 'abcabc'
"abc".repeat(3.5); // 'abcabcabc' (count will be converted to integer)
"abc".repeat(1 / 0); // RangeError
({ toString: () => "abc", repeat: String.prototype.repeat }).repeat(2);
// 'abcabc' (repeat() is a generic method)
规范
Specification |
---|
ECMAScript Language Specification # sec-string.prototype.repeat |
浏览器兼容性
BCD tables only load in the browser