1

I am new in JScript world and trying to understand this code. But could not understand it. here function only accept a number as parameter, but how he returns the string as he does not receive the string as the parameter. I will geld if somebody please explain this in detail. Besides, I will be glad if somebody also explains what this is doing here?

String.prototype.repeatify = function(numTimes) {
  var strArray = [];
  for (var i = 0; i < numTimes; i++) {
    strArray.push(this);
  }
  return strArray;
};
console.log('hello'.repeatify(5));

I have one more question, what if i save this function in a variable like this :

var someName =  String.prototype.repeatify = function (x) {}

then how could I use this someName function that will do the same.

Sanjida lina
  • 113
  • 10

0 Answers0