函数参数与返回值 |模板字面量与模板函数
*函数的参数和返回值
<!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>
// 常规参数
let fn = (a, b) => a + b;
console.log(fn(1, 5));
// 参数不足 给参数默认值
let f = (a, b = 1) => a * b;
console.log(f(5));
// 参数过多,用...arr接收
f = (a, b, ...c) => console.log(a, b, c);
console.log(f(1, 2, 3, 4, 5, 6));
let arr = [1, 2, 3, 4, 5, 6];
console.log(...arr);
//返回多个值用数组或对象
//数组
let fnn = () => [1, 2, 3, 4];
console.log(fnn());
//对象
fnnn = () => ({
id: 1,
name: "张老师",
age: 33,
});
console.log(fnnn());
</script>
</body>
</html>
*模板字面量与模板函数
<!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`);
let name = "张老师";
console.log(`Hello` + name);
//也可以如下 用‘${xxxx}’表示占位符
console.log(`Hello ${name}`);
//也可以用表达式
let age = 1;
console.log(`${age ? `男:${name}` : `女`}`);
//模板函数,用模板字面量当参数时 就是模板函数
price`数量:${20}单价:${50}`;
function price(strings, ...args) {
console.log(strings);
console.log(args);
console.log(`总价:${args[0] * args[1]}元`);
}
</script>
</body>
</html>