12-29: 字符串和函数

77 阅读1分钟

字符串:

// let str = "\uD842\uDFB7"; 单字节

    // let str = "\u{20BB7}"; 双字节

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

    // for (const item of str) {
    //     console.log(item);
    // }

    // console.log(str);

    // const str2 = `str的值是:$(str)`;

    let str2 = String.raw`hi \n${str}`
    console.log(str2)

函数:

// function f() { // console.log("f"); // } // f();

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

    // let f = () => { };
    let sum = (a, b) => { return a + b };
    // ()小括号 有且只有一个参数的时候 可以省略
    // {}大括号 有且只有一条语句的时候 可以省略 函数会自动返回函数体的计算结果 不能使用return
    let sum = (a, b) => a + b;
    let add = () => console.log(0);
    console.log(sum(1, 2));