
批改状态:合格
老师批语:
(1) 参数不足要设置默认参数,否则出现NaN
let f = (a,b) => a+b;
console.log(f(10,20));
console.log(f(10));
(2) 参数过多,用 … 接收剩余参数
f = (a,b) => console.log(a,b);
f(1,2,3,4,5);
f = (a, b, ...c) => console.log(a,b,c);
f(1,2,3,4,5);
(3)…用在参数调用时的实参中,是解包,打散
let arr = [1,2,3,4,5];
console.log(...arr);
f(...arr);
(4)函数只能有一个返回值,默认单值返回
let fn = () => [1, 2, 3];
let res = fn();
console.log(res);
(5) 函数需要返回多个值怎么办? 可以使用 引用类型 的复合值
let fn2 = () => [1, 2, 3];
let res2 = fn2();
console.log(res2);
fn3 = () => ({
id: 2,
name: 'admin',
age: 28,
});
res3 = fn3();
console.log(res3);
(1)模板字面量
let name = '小明';
console.log('hello ' + name);
console.log(`hello ${name}`);
let gender = true;
console.log(`${gender ? `男:${name}`:`女`}`);
(2)模板函数
calc`数量: ${10}单价: ${500}`;
function calc(strings, ...args) {
console.log(strings);
console.log(args);
}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号