写一个方法,里面传一个参数,让字符串 可以返回 这个参数的次数。

1:用怎样的函数写?
2:怎样把这个字符串本身传进去?

3:把得到的结果连起来成整体?

1种

 String.prototype.repeat=function(item){
var arr=[];
for(i=0;i<item;i++){
arr[i]=this.tostring;//这个this指的是谁,tostring把它变成了什么?
}
returen arr.join(" ");
}
console.log("hellow".repeat(3));

2种

String.prototype.repeat=function(item){
       var str="";//这样声明的用处
  for(i=0;i<item;i++){
     str+=this;//这个this指的是谁,是什么?
}
     returen str;
}
  console.log("hellow".repeat(3));
原文地址:https://www.cnblogs.com/S-Ekin/p/6568404.html