1. 使用while循环
let str = 'abc'
console.log(str.repeat(2));
String.prototype._repeat = function () {
let res = '', i = 0
while (i < arguments[0]) {
res += this.toString()
i++;
}
return res
}
console.log(str._repeat(3));
2. 插空法
let str = 'abc'
console.log(str.repeat(2));
String.prototype._repeat = function () {
return (new Array(arguments[0] + 1)).join(this.toString());
}
console.log(str._repeat(3));