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