es6学习笔记(二)——字符串扩展

1.includes() 返回布尔值,表示是否找到了参数字符串

var s = 'hello  world!"

s.includes("o")  //true

2、startsWith() 返回布尔值,表示数字字符串是否在源字符开头

var s = "Hello world"

s.startsWith('Hello')

3、endsWidth() 返回布尔值,表示参数字符串是否在源字符串的尾部

var s= "Hello world !";

s.endsWith("!");

4、repeat返回一个新的字符串,表示将原字符串重复n次,不能是负数,如果是小数就会被取整

'x'.repeat(3) //'XXX'

5、模板字符串

传统的js输出模板通常是这样写的

$('#box').append(
  'There are <b>' + basket.count +'</b>' +
  'items in your basket,' +
   '<em>' + basket.onSale + '</em> are on sale!'
);

  ES6模板字符串的写法

$('#box').append(`
   there are <b> ${basket.count}</b> items
   in your basket,<em>${basket.onSale}</em>
   are on sale!
`);

(今天更新到此)

猜你喜欢

转载自blog.csdn.net/caimaomaocai/article/details/82963041