一、模板字符串
1.模板字符串中可以解析变量
let name = ‘张三‘;
let sayHello = `hello,my name is ${name}`; // hello, my name is zhangsan
2.模板字符串中可以换行
let result = {
name: ‘zhangsan‘,
age: 20,
sex: ‘男‘
}
let html = ` <div>
<span>${result.name}</span>
<span>${result.age}</span>
<span>${result.sex}</span>
</div> `;
3.在模板字符串中可以调用函数
const sayHello = function () {
return ‘哈哈哈哈 追不到我吧 我就是这么强大‘;
};
let greet = `${sayHello()} 哈哈哈哈`;
console.log(greet); // 哈哈哈哈 追不到我吧 我就是这么强大 哈哈哈哈
二实例方法:startsWith() 和 endsWith()
let str = ‘Hello world!‘;
str.startsWith(‘Hello‘) // true
str.endsWith(‘!‘) // true
‘x‘.repeat(3) // "xxx"
‘hello‘.repeat(2) // "hellohello"
原文:https://www.cnblogs.com/kawayi/p/13934335.html