有关ECMAScript定义如何获取this请移步ECMAScript中关于如何获取this的定义
绝大多数情况下,函数的调用方式决定了this的取值
全局上下文
console.log(this === window); //true
函数上下文
直接调用
function fn(){
return this;
}
fn() === window; //true
//this的值不是由函数调用设定,默认为全局对象
严格模式下增强了安全措施,this关键字禁止指向全局对象
function fn(){
"use strict";
return this;
}
fn() === undefined; //true
function f(){
"use strict";