模板字面量与模板函数实例
发布于:2022-04-02 12:27:54
次阅读
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>模板字面量与模板函数</title></head><body> <script> console.log('Hello world'); // 反引号:支持在字符串插入变量/表达式:插值 console.log(`Hello world`); let name = '小鸟'; console.log(`Hello ${name}`); let gender = 1; console.log(`${gender ? '男' : '女'}`); console.log(`${gender ? `男:{name}` : '女'}`); //多行连续字符串的写法 console.log(` asdf asdf adsf asdf asdf `); // 模板函数 //使用模板字面量为参数的函数 // alert(`Hello php.cn`); // alert `Hello php.cn`; calc `数量:${20} 单价:${500}`; // 模板函数的参数: // 第一个参数:模板字面量中的”字符串字面量“ // 第二个参数:插值组成的数组 function calc(strings, ...args){ console.log(strings); console.log(args); console.log('总金额:'+args[0]*args[1]); } </script></body></html>