一.函数中this是什么?
(任何函数本质上都有通过某个对象来调用的,如果没有直接指定就是window)
1.所有函数内部都有一个变量this
2.她的值是调用函数的当前对象
二.如何确定this的值?
1.test() this的值是window
2.p.test() this的值是p
4.new test() this的值是新创建的对象
5.p.call(object) 这个时候是object
注:用箭头函数声明时,this始终指向函数声明时所在作用域下的this的值
let getname1=function(){
console.log(this.name)
}
let getname2=()=>{
console.log(this.name)
}
window.name='Bob'
const school={
name:'AT
}
getname1.call(school) //输出AT
getname2.call(school) //输出Bob