this的指向问题

1.全局执行环境

1.1非严格模式 为window

1
console.log(this)

1.2严格模式 为window

1
2
 'use strict'
console.log(this)

2.直接调用

2.1非严格模式 为window

1
2
3
4
function func() {
console.log(this);
}
func()

2.2严格模式 为undefind

1
2
3
4
5
function func() {
'use strict'
console.log(this)
}
func()

3.对象调用

3.1非严格模式 为调用的对象

1
2
3
4
5
6
7
const food = {
name:'小面',
eat() {
console.log(this)
}
}
food.eat()

3.2严格模式 为调用的对象

1
2
3
4
5
6
7
8
const food = {
name:'小面',
eat() {
'use strict'
console.log(this)
}
}
food.eat()