//箭頭函數:是一種定義函數的方式 //當我們需要把一個函數作為返回值傳給一個函數時通常會用到箭頭函數 //兩個參數 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。”)