1.全局作用域的this 指向
console.log(this);//window
2.一般函数(非箭头函数)中的this 指向
1.只有在函数调用的时候this 指向菜确定,不调用的时候,不知道指向谁
2.this 指向和函数 在哪儿 调用没关系,只和谁在调用有关
一般普通函数谁调用我就指向谁
事件处理中this,指向当前事件的触发对象
构造函数this: 指向实例化生成的对象
function PerSon(username){ this.username=username; console.log(this) //p } const p=new PerSon("Alex");
箭头函数是没有this 指向的 :箭头函数没有自己的this
const calc ={ add:()=>{ console.log(this) } } calc.add() //window
练习:
const calc ={
add:function(){
const adder=()=>{
console.log(this) //calc
}
adder() //箭头函数调用,箭头函数是没有this 指向的,要往上一层作用域查找
}
}
calc.add() //普通函数谁调用 里面的 this 就指向谁
const addFn=calc.add //没有调用,复制给一个新的函数
addFn() //windown