this
文章目录
this
this是当前行为的执行主体。
this与函数在哪定义、在哪执行都没关系。
如何区分this
1. 函数执行,首先看函数名前面有没有"."
有".",this就是"."前面的;
没有".",this就是window。
function fn() {
console.log(this)
}
var obj = {
fn: fn
}
fn(); //this => window
obj.fn(); //this => obj
this是当前行为的执行主体。
this与函数在哪定义、在哪执行都没关系。
有".",this就是"."前面的;
没有".",this就是window。
function fn() {
console.log(this)
}
var obj = {
fn: fn
}
fn(); //this => window
obj.fn(); //this => obj
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论