It's very simple.
When you alter the prototype of something, in this case the default String class, you make that method available to any instance of that class, in this case to any string.
Now let's look at what the method does.
return new Array(num + 1).join(this);
Because of String.prototype.displayDate = function(num) {, the value of this inside that function is the value of the string. The this reference points to the current object, I guess that makes sense.
Then it's creating an array of num + 1 elements, which will all be initialised with undefined. Array.prototype.join returns the string representation of the array elements separated by the thing you provide as an argument to it.
in this case, you have an array of num +1 undefined values. the string representation of undefined is "", or the empty string. So you end up having num + 1 concatenations of the empty string + the value of this, or num times your string.
Say your string is "test", and you call repeat(2).
It first created a = new Array(undefined, undefined, undefined);// 2 + 1 times.
Then it starts to join the strings, putting "test" in between each pair.
new String(undefined) + new String("test") + new String(undefined); + new String("test") + new String(undefined)
The above becomes:
"" + "test" + "" + "test" + "" = "testtest"// two times the original string.
displayToday()anddisplayDate()?