说,在这种情况下:这个.join(这个)如何在javascript中工作?
String.prototype.times = function(count) {
return count < 1 ? '' : new Array(count + 1).join(this);
}
"hello!".times(3); //"hello!hello!hello!";
"please...".times(6); //"please...please...please...please...please...please..."
它是如何添加到新语句3倍?我在理解return语句时也有些困惑。请告诉我,如果我正确理解这一点:
(if count < 1){
return ''
} else {
return new Array(count + 1).join(this) //This I don't understand.
谢谢。
啊我明白了。但为什么它必须有7的给定长度?数组是否连接到原始字符串,因为它是.join(this)? – Sean 2012-08-06 21:13:25
@SeanDokko'7'就是一个例子,你可以在那里传递任何数字作为参数。如果我说'.times(15)',那么'count + 1'等于16,这就形成了一个“新的Array(16)”,然后将15个字符串连接在一起。创建的数组不会附加任何东西,它会创建并丢失。 – Esailija 2012-08-06 21:13:42
我明白了。感谢您的进一步澄清。 – Sean 2012-08-06 21:15:20