A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/repeat below:

String.prototype.repeat() - JavaScript | MDN

String.prototype.repeat()

Baseline Widely available

repeat() 方法构造并返回一个新字符串,其中包含指定数量的所调用的字符串副本,这些副本连接在一起。

尝试一下
const mood = "Happy! ";

console.log(`I feel ${mood.repeat(3)}`);
// Expected output: "I feel Happy! Happy! Happy! "
语法 参数
count

介于 0 和 +Infinity 之间的整数。表示在新构造的字符串中重复了多少遍原字符串。

返回值

包含指定字符串的指定数量副本的新字符串。

异常
RangeError

如果 count 为负值,或者 count 超过了字符串的最大长度,将抛出错误。

示例 使用 repeat()
"abc".repeat(-1); // RangeError
"abc".repeat(0); // ''
"abc".repeat(1); // 'abc'
"abc".repeat(2); // 'abcabc'
"abc".repeat(3.5); // 'abcabcabc'(count 将被转换为整数)
"abc".repeat(1 / 0); // RangeError

({ toString: () => "abc", repeat: String.prototype.repeat }).repeat(2);
// 'abcabc'(repeat() 是一个通用方法)
规范 浏览器兼容性 参见

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4