Say in this context:
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..."
How does it add on to the new statement 3 times? I’m also having some confusion in understanding the return statement. Please tell me if I am understanding this correctly:
(if count < 1){
return ''
} else {
return new Array(count + 1).join(this) //This I don't understand.
Thank you.
It makes a new array of given length, say 7. Then you join all those empty items with a string, which ends up repeating that string 6 times.
Normally:
Then with an array of length 4:
thisinside aString.prototypemethod refers to the string object the function was called as a method on: