Skip to content

Latest commit

 

History

History
31 lines (22 loc) · 498 Bytes

js函数的this指向.md

File metadata and controls

31 lines (22 loc) · 498 Bytes

setTimeout

setTimeout(function(){
    console.log(this) // Window
})

数组中的函数

forEach传入的函数,执行时this默认指向Window

let arr = [1,2,3,4,5]
arr.forEach(function(itme){
    console.log(this) // Window
})

forEach的第二个参数可以改变this的指向

let arr = [1,2,3,4,5]
arr.forEach(function(itme){
    console.log(this)  // {'a':'b'}
}, {'a':'b'})

map/filter/find 都是相同的情况