打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
ES6扩展

模板字符串和标签模板

const getCourseList = function() {        // ajax        return {            status: true,            msg: '获取成功',            data: [{                id: 1,                title: 'Vue 入门',                date: 'xxxx-01-09'            }, {                id: 2,                title: 'ES6 入门',                date: 'xxxx-01-10'            }, {                id: 3,                title: 'React入门',                date: 'xxxx-01-11'            }]        }    };    const { data: listData, status, msg } = getCourseList();    function foo(val) {        return val.replace('xxxx', '2020');    }    if (status) {        let arr = [];        listData.forEach(function({ date, title }) {            arr.push(                `                    <li>                        <span>${ `课程名: ${ title }` }</span>                        <span>${ foo(date) }</span>                    </li>                `            );        });        let ul = document.createElement('ul');        ul.innerHTML = arr.join('');        document.body.appendChild(ul);    } else {        alert(msg);    }

padStart padEnd

    {        let str = 'i';        // 插入在5的位置        let str1 = str.padStart(5, 'mooc');        console.log(str1);        // 倒序插入在5的位置        let str2 = str.padEnd(5, 'mooc');        console.log(str2);    }

repeat

    {        function repeat(str, num) {            return new Array(num   1).join(str);        }        console.log(repeat('s', 3));    }

startsWith endsWith

    {        const str = 'A promise is a promsie';        console.log(str.startsWith('B'));        console.log(str.startsWith('A pro'));        console.log(str.endsWith('promsie'));        console.log(str.endsWith('A'));    }

includes

    {        const str = 'A promise is a promise';        if (~str.indexOf('promise')) {            console.log('存在"promise"');        }        if (str.includes('a promise')) {            console.log('存在"a promise"');        }    }

字符串转数组 遍历

    let str = 'PROMISE';    // 四种方法:转成数组后遍历    var oStr = Array.prototype.slice.call(str);    var oStr = str.split('');    const oStr = [...str];    const [...oStr] = str;    console.log(oStr);    //单个字符输出    oStr.forEach(function(word) {        console.log(word);    });

对全是英文的字符串中的大写字符加密 A -> 100  B -> 99

    const map = {A: '100', B: '99', C: '98', D: '97', E: '96', F: '95', G: '94', H: '93', I: '92', J: '91', K: '90', L: '89', M: '88', N: '87', O: '86', P: '85', Q: '84', R: '83', S: '82', T: '81', U: '80', V: '79',W: '78',X: '77',Y: '76', Z: '75'};    const words = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';    oStr.forEach(function(word, index) {        if (words.includes(word)) oStr[index] = map[word];    });    console.log(oStr.join(''));

使用for-of遍历字符串

    let str = 'PROMISE';    let newStr = '';    const map = {A: '100', B: '99', C: '98', D: '97', E: '96', F: '95', G: '94', H: '93', I: '92', J: '91', K: '90', L: '89', M: '88', N: '87', O: '86', P: '85', Q: '84', R: '83', S: '82', T: '81', U: '80', V: '79',W: '78',X: '77',Y: '76', Z: '75'};    for (let word of str) {        if (str.includes(word)) newStr  = map[word];    }    console.log(newStr);

Unicode是一项标准,包括字符集、编码方案等
解决传统的字符编码方案的局限

来源:https://www.icode9.com/content-4-728201.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
熬夜7天,我总结了JavaScript与ES的25个知识点
俺たちはJavaScriptの非同期処理とどう付き合っていけば良いのだろうか
ES6之变量常量字符串数值
解决异步的几种实现方式
函数的使用
js数组循环的几个常用方法比较(for、forEach、map)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服