說,在這種情況下:這個.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