严格模式下this绑定更明确:独立调用时为undefined,构造函数不加new会报错,call/apply/bind传null/undefined时this保持原值,对象方法提取后调用也返回undefined,提升可预测性。

在严格模式下,this 的绑定规则变得更明确、更可控,不再默认指向全局对象,而是保持为 undefined(当函数独立调用时)。
独立函数调用时 this 为 undefined
非严格模式下,全局环境下直接调用函数,this 指向 window(浏览器)或 global(Node.js);严格模式下,此时 this 是 undefined,避免意外污染全局对象。
- 例如:
function foo() { console.log(this); },严格模式下执行foo()输出undefined - 这能提前暴露未正确绑定
this的问题,比如忘记用call/bind或箭头函数
构造函数必须用 new 调用
严格模式下,若函数被设计为构造函数(如首字母大写),但被普通方式调用(没加 new),this 不再指向全局,而是 undefined,从而让错误立即浮现。
- 例如:
function Person(name) { this.name = name; },严格模式下Person('Alice')会报错(Cannot set property 'name' of undefined) - 而非严格模式下,
this指向全局,导致意外挂载属性(如window.name = 'Alice')
call、apply、bind 传入 null/undefined 时 this 不再自动转为全局对象
严格模式下,显式传入 null 或 undefined 作为 thisArg,this 就真的保持为 null 或 undefined;非严格模式会自动替换为全局对象。
-
function f() { return this; },严格模式下f.call(null)返回null - 非严格模式下同样调用返回
window(或global),容易掩盖逻辑意图
对象方法中 this 仍正常绑定,但更“诚实”
对象方法调用(如 obj.method())的 this 绑定行为本身不变,但严格模式消除了歧义场景——比如把方法单独提取后调用,会明确得到 undefined 而不是隐式全局绑定。
-
const obj = { fn() { return this; } };,obj.fn()中this仍是obj - 但
const fn = obj.fn; fn();在严格模式下this是undefined,而非全局对象
这些改变让 this 的行为更可预测,减少隐蔽 bug,也促使开发者主动管理上下文绑定。











