1、ES6允许使用箭头 => 定义函数
- 声明一个函数
let fn = function(){
}
let fn = (a,b) => {
return a + b;
}
- 调用函数
let result = fn(1, 2);
console.log(result);
2、this 是静态的. this 始终指向函数声明时所在作用域下的 this 的值
function getName() {
console.log(this.name);
}
let getName2 = () => {
console.log(this.name);
}
window.name = 'mumuwei';
const school = {
name: "MUMUWEI"
}
- 直接调用
getName();
getName2();
- call 方法调用
getName.call(school);
getName2.call(school);
let Person = (name, age) => {
this.name = name;
this.age = age;
}
let me = new Person('xiao',30);
console.log(me);
3、不能使用 arguments 变量
let fn = () => {
console.log(arguments);
}
fn(1,2,3);
4、箭头函数的简写
- 省略小括号, 当形参有且只有一个的时候。
let add = n => {
return n + n;
}
console.log(add(9));
- 省略花括号, 当代码体只有一条语句的时候, 此时 return 必须省略,而且语句的执行结果就是函数的返回值。
let pow = n => n * n;
console.log(pow(8));