字符串和函数

154 阅读1分钟

ES6 加强了对 Unicode 的支持,允许采用\uxxxx形式表示一个字符,其中xxxx表示字符的 Unicode 码点。

字符串的遍历器接口

ES6 为字符串添加了遍历器接口(详见《Iterator》一章),使得字符串可以被for...of循环遍历。

 <script>
        // let str="\uD847";
        let str="\u{20BB7}";

        // for (let i = 0; i < str.length; i++) {
        //     console.log(str[i]);
        // }
        // for (const item of str) {
        //     console.log(item);
        // }

        // let str="\u4e00";
        // console.log(str);
        // const str2=`str的值是:${str}`;

        let str2=String.raw`hi\n${str}`;

    </script>

函数: ES6 允许为函数的参数设置默认值,即直接写在参数定义的后面。

 <script>
        function f(){
            console.log("f");
        }
        f();

        let f=function(){
            console.log("f");
        }
        f();

        // let f=(参数)=>{函数题}  箭头函数
        // let sum=(a,b)=>{return a+b};
        // ()有且只有1个参数的时候  可以省略
        // {}有且只有1条return语句的时候  可以省略 单条语句的return必须省略
        let sum=(a,b)=>a+b;
        let add=()=>console.log(0);
        console.log(sum(1,2));
    </script>