this 指向
this指向
全局上下文
console.log(this === window);//node环境是global函数调用(简单调用)
function f1(){
return this;
}
//在浏览器中:
f1() === window; //在浏览器中,全局对象是window
//在Node中:
f1() === global;
function f2(){
"use strict"; // 这里是严格模式
return this;
}
f2() === undefined; // true作为对象的方法(对象方法调用)
作为一个DOM事件处理函数
作为一个内联事件处理函数
箭头函数
参数为函数的情况
改变this指向(apply、call、bind)
QA
addEventListener与on-event的概念及区别
概念
区别
addEventListener中回调使用箭头函数与普通函数,this指向不同
Last updated