1、函数预编译过程this指向window
2、全局作用域里this指向window
3、call/apply 可以改变函数运行时this指向
4、obj.func( ); func( )里面的this指向obj
5、严格模式下的this指向undefined
6、内置函数,如setTimeout中的this一般指向window
7、回调函数,如arr.filter(function(item){})中的this一般指向window
8、自执行函数中的this指向window
实例1:
function f1( ){console.log(this)}
var arr = [f1, 2, 3]
arr[0]( ) // this指向arr
f1( ) // this指向window
实例2:
function f2( ){console.log(this)}
setTimeout(f2, 1000) // window
实例3:
[1, 2, 3, 4].filter(function(item){
console.log(this) // window
return item
})
实例4:
var num = 1
var obj = {
num : 2,
showNum : function(){
this.num = 3;
(function(){
console.log(this.num) // 1
}( ))
console.log(this.num) // 3
}
}
obj.showNum( )