模版字符串的函数特性

模版字符串的函数特性

徐徐
前端
发布于2024-11-15 17:24:52
🌺前言
你不知道的模板字符串

javascript
      function customTag(strings, ...values) {
            console.log(strings,values);// ["Name: ",", Age: ",""] ["Alice",30]
            let result = "";
            strings.forEach((string, index) => {
                result += string;
                if (index < values.length) {
                    result += values[index];
                }
            });
            return result.toUpperCase();
        }

        const name = "Alice";
        const age = 30;

        const formattedString = customTag`Name: ${name}, Age: ${age}`;

        console.log(formattedString); // 输出 "NAME: ALICE, AGE: 30"
文章最后更新于 2024-11-15 17:24:52
留言
暂无数据

~~空空如也