ES6String 的扩展方法

一、模板字符串
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()

  1. startsWith():表示参数字符串是否在原字符串的头部,返回布尔值
  2. endsWith():表示参数字符串是否在原字符串的尾部,返回布尔值
let str = 'Hello world!';
str.startsWith('Hello') // true 
str.endsWith('!')       // true
  1. repeat方法表示将原字符串重复n次,返回一个新字符串
'x'.repeat(3)      // "xxx" 
'hello'.repeat(2)  // "hellohello"
原文地址:https://www.cnblogs.com/kawayi/p/13934335.html