加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ES6扩展

发布时间:2020-12-16 04:21:52 所属栏目:百科 来源:网络整理
导读:模板字符串和标签模板 const getCourseList = function () { // ajax return { status: true ,msg: '获取成功' 1 'Vue 入门' 'xxxx-01-09' },{ id: 2 'ES6 入门' 'xxxx-01-10' 3 'React入门' 'xxxx-01-11' }] } }; const { data: listData,status,msg } = ge

模板字符串和标签模板

const getCourseList = function() {
        // ajax
        return {
            status: true,msg: '获取成功''Vue 入门'
            },{
                id: 2'xxxx-01-10''React入门'
            }]
        }
    };

    const { data: listData,status,msg } = getCourseList();

     foo(val) {
        return val.replace('xxxx','2020');
    }

    if (status) {
        let arr = [];

        listData.forEach(({ 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,1)">);
        console.log(str2);
    }

repeat

    {
         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((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((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',1)">};
    for (let word of str) {
        if (str.includes(word)) newStr += map[word];
    }
    console.log(newStr);

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读