// 声明:
let fun = (a, b) => {
return a + b;
}
// 调用
fun(1, 2) // 3
特点
箭头函数 this 指向声明时所在作用域下 this 的值,不会改变
function getNum1 () {
console.log(this.number);
}
let getNum2 = () => {
console.log(this.number);
}
var number = 1;
var obj= {
number: 2
}
getNum1() // 1
getNum2() // 1
getNum1.call(obj) // 2
getNum2.call(obj) // 1 由于箭头的this指向 getNum2所在的区域,全局作用域中,所以始终输出 1
如果形参只有一个,则小括号可以省略
函数体如果只有一条语句,则花括号可以省略,函数的返回值为该条语句的
let fun = num => num / 100 + '元';
箭头函数不能作为构造函数,不能被实例化(会报错)
let Person = (name, age) => {
this.name = name;
this.age = age;
}
let person1 = new Person('张三', 18);
console.log(person1) // Uncaught TypeError: Person is not a construtor