function f1(){
console.log(this) //普通函数中的this是window
}
window.f1() //只是我们调用函数的时候省略了this
window.setInterval(()=>{
console.log(this) //定时器中的this是window,也是window被我们省略了
},5000)
function Per(name){
this.name = name //构造函数当中的this是实例p
}
let p = new Per("yuchunhui")
Per.prototype.eat = function(){
console.log(this) //这里的this也是实例p
}
本文深入探讨JavaScript中this关键字的指向规则,包括普通函数、构造函数、原型方法等不同上下文中this的具体表现,揭示其背后的运行机制。
286

被折叠的 条评论
为什么被折叠?



