写法
// 只有一个参数
var f = a => a;
var f = function(a) {
return a;
};
var f = () => 1;
function f() {
return 1;
}
let f = (a, b) => {
return a + b;
};
const full = ({first, last} = {}) => first + ' ' + last;
console.log(full({first: 3, last: 5})); // 35
var arr = [123,4,55,6,7,67,4,56,34,3,54,666,3,7];
var arr1 = arr.sort((a, b) => a - b);
console.log(arr1);
⭐️ 箭头函数是匿名函数,不能作为构造函数 new
var B = () => {
value:1;
}
var b = new B(); //-->TypeError: B is not a constructor
⭐️ 箭头函数中没有 arguments 对象
var sum = (a, b) => {
console.log(arguments);
return a + b;
}
sum(1, 3);
... spread/rest 运算符(展开或收集)
收集
var sum = (...args) => {
console.log(args);
console.log(args[0] + args[1]);
}
sum(1, 2);
⭐️ rest 收集,必须是最后一个参数
let fn = (a, b, ...c) => {
console.log(a, b, c);
}
fn(1, 2, 3, 4, 5);
展开
function foo(x, y, z) {
console.log(x, y, z);
}
foo(...[1, 2, 3]);