Open dashengzi66 opened 3 years ago
全局上下文中的this:window 块级上下文中没有自己的this,所有的this都是继承上级上下文中的this(箭头函数也是)
正常的普通函数执行:看函数执行前是否有“点”,如果有“点”,“点”前面是谁this就是谁,没有this是window(严格模式下是undefined)
function fn() { console.log(this); } let obj = { name:'asever6', fn } fn();//this->window obj.fn();//this-obj
匿名函数: 函数表达式:等同于普通函数或者事件绑定等机制 自执行函数:this一般都是window/undefined (function(x){console.log(this)})(10);//window/undefined 回调函数:一般都是window/undefined,但是如果在函数执行中,对回调函数的执行做了特殊处理,以自己的处理为主 括号表达式:小括号中包含“多项”,这样也只取最后一项,但是this受到影响(一般是window/undefined) (obj.fn)();//this->obj (10,obj.fn)();//this->window(严格模式下是undefined)
this:函数执行的主体(谁执行的函数)
全局上下文中的this:window 块级上下文中没有自己的this,所有的this都是继承上级上下文中的this(箭头函数也是)
事件绑定
函数执行
正常的普通函数执行:看函数执行前是否有“点”,如果有“点”,“点”前面是谁this就是谁,没有this是window(严格模式下是undefined)
匿名函数: 函数表达式:等同于普通函数或者事件绑定等机制 自执行函数:this一般都是window/undefined (function(x){console.log(this)})(10);//window/undefined 回调函数:一般都是window/undefined,但是如果在函数执行中,对回调函数的执行做了特殊处理,以自己的处理为主 括号表达式:小括号中包含“多项”,这样也只取最后一项,但是this受到影响(一般是window/undefined) (obj.fn)();//this->obj (10,obj.fn)();//this->window(严格模式下是undefined)