es6字符串扩展(+模板字符串拼接)


includes()
判断字符串中是否包含指定的字串(有的话返回true,否则返回false)
console.log('hello world'.includes('world' , 7));

//参数一:匹配的字串;参数二:从第几个开始匹配
startsWith()  判断字符串是否以特定的字串开始
endsWith()   判断字符串是否以特定的字串结束
let url = 'admin/index.php';

console.log(url.startsWith('admin'));
//返回true

console.log(url.endsWith('php'));
//返回true

模板字符串拼接

原始的:

let tag = '<div><span>'+obj.username+'</span><span>'+obj.age+'</span><span>'+obj.gender+'</span></div>';

console.log(tag);

高级的:

// 用反引号表示模板,模板中的内容可以有格式,通过${}方式填充数据
let fn = function(info){
    return info;
}

let tpl = `
    <div>
        <span>${obj.username}</span>
        <span>${obj.age}</span>
        <span>${obj.gender}</span>
        <span>${1+1}</span>
        <span>${fn('nihao')}</span>
    </div>
`;

console.log(tpl);

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM