首页 > 其他 > 详细

ES6String 的扩展方法

时间:2020-11-06 00:15:16      阅读:33      评论:0      收藏:0      [点我收藏+]

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

ES6String 的扩展方法

原文:https://www.cnblogs.com/kawayi/p/13934335.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!