1、谁调用该函数this指向就指向谁
2、回调函数中this的指向永远都指向window
3、箭头函数指向最近的作用域,箭头函数本身是没有this的指向
4、定时器永远指向window
5、严格模式下函数指向undefined,除了箭头函数和定时器以外,箭头函数是没有arguments,因此在ES6中应该的是扩展运算符
setTimeout(function() {
console.log(this)
}, 0)
==》window
function fn(){
console.log(this)
}
fn();
==》window
document.onclick = function(){
fn();
}
==》window
var obj = {
name:"123",
age:19,
show(){
console.log(this);
}
}
obj.show();
==》obj
var obj = {
name:"123",
age:19,
show(){
(function(){
console.log(this)
})()
}
}
obj.show();
==》window
var obj = {
name:"123",
show(){
var fn = ()=>{
console.log(this);
}
fn();
}
}
obj.show();
==》obj
class Person{
constructor(name){
this.name = name;
}
show(){
console.log(this);
}
}
var p = new Person("张三");
p.show();
==》指向实例化对象
"use strict"
var fn = function() {
console.log(this);
}
fn();
==》undefined
setTimeout(function(){
console.log(this);
},0)
==》window