码迷,mamicode.com
首页 > 其他好文 > 详细

看es6 字符串新方法有感

时间:2016-09-01 14:25:33      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:

‘x‘.repeat(3) // "xxx"
‘hello‘.repeat(2) // "hellohello"
‘na‘.repeat(0) // ""

  

‘x‘.padStart(5, ‘ab‘) // ‘ababx‘
‘x‘.padStart(4, ‘ab‘) // ‘abax‘

 

  上面是es6的新方法

es5 prototype可以写出 无聊写了点

String.prototype.repead=function(n){
  var arr=new Array()
  arr.length=parseInt(n+1)
  return arr.join(this)
}
String.prototype.buquan=function(n,str){
  var t=this
  if(t.length<n){
   return str.repead(Math.floor((n-t.length)/str.length)+1).substring(0,n-t.length)+t
  }else{
    return t.toString()
  }
}

  es5写出

‘x‘.repead(3) // "xxx"
‘hello‘.repead(2) // "hellohello"
‘na‘.repead(0) // ""

‘x‘.buquan(5, ‘ab‘) // ‘ababx‘
‘x‘.buquan(4, ‘ab‘) // ‘abax‘

  

看es6 字符串新方法有感

标签:

原文地址:http://www.cnblogs.com/yunyi1895/p/5829508.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!