//箭头函数:是一种定义函数的方式
//当我们需要把一个函数作为返回值传给一个函数时通常会用到箭头函数
//两个参数
const sum = (num1, num2) => {
return num1 + num2;
}
//一个参数的时候是可以省略括号的
const fun = (num) => {
return num * num
}
const fun2 = num => {
return num * num
}
//函数中的代码情况2
//多行代码
const test = () => {
console.log('da')
console.log('da')
}
//一行代码
const test2 = () => {
console.log('da')
}
//省略大括号
const test3 = () => console.log('da')
//箭头函数中this的使用
let fun1 = setTimeout(() => {
console.log(this) //window
}, 1000);
let fun2 = setTimeout(function() {
console.log(this) //window
}, 1000);
const obj = {
bbb() {
setTimeout(function() {
console.log(this) //window
}, 1000)
},
aaa() {
setTimeout(() => {
console.log(this) //obj
}, 1000)
}
}
//箭头函数中的this引用的是向外层作用域中一层层查找this直到有this
//而在普通函数中 函数执行时,实际是window调用了它,也就是window.函数名();那么,里面的this指向当前调用该函数的对象,就是window。”)