JavaScript全局环境中this默认指向全局对象:浏览器中为window,Node.js模块顶层为module.exports;严格模式下为undefined;箭头函数不绑定this,继承外层作用域的this值。

在 JavaScript 全局环境中,this 默认指向全局对象。
浏览器环境中的 this
在浏览器中,全局对象是 window。因此,在非严格模式下,直接在全局作用域中访问 this,它就等于 window:
console.log(this === window); // truethis.name = "global"; console.log(window.name); // "global"
Node.js 环境中的 this
在 Node.js 的模块顶层(即 .js 文件的最外层),this 指向当前模块的 module.exports,而不是 global 对象:
console.log(this === module.exports); // truethis.foo = 123; console.log(module.exports.foo); // 123
注意:虽然 global 是 Node.js 的全局对象,但全局作用域里的 this 并不等于 global(除非显式绑定或在函数中以非严格模式调用)。
在 Java 中初始化和管理阿里云 SDK客户端。包括单例模式、线程安全、endpoint 与 region 配置、VPC 终端节点、同步与异步等。
立即学习“Java免费学习笔记(深入)”;
严格模式的影响
在严格模式("use strict")下,全局作用域中的 this 值为 undefined:
"use strict"; console.log(this); // undefined- 这与非严格模式形成明显区别,也避免了意外绑定到全局对象的问题。
箭头函数不绑定 this
箭头函数没有自己的 this,它会沿作用域链向上查找外层普通函数或全局环境的 this 值:
- 在全局作用域定义的箭头函数,其 this 就是全局环境的 this(浏览器中为 window,严格模式下为 undefined)。
const fn = () => console.log(this); fn(); // 输出同上

















